Script problem

THIS IS MY CODE:

local propCube = script:GetCustomProperty("Cube")
local cube = World.SpawnAsset(propCube)
cube.collision = 2

cube:AttachToPlayer(player, "head")

Error running Lua task: [E745F7DC8279E46F] AttachToPlayer:5: stack index 2, expected Player, received nil: (bad argument into 'void(CoreObject, Player, string)')

why this cant be done?i follow the API examples,help me!

Check the last line. There is a variable named "player". You have to define that before giving as a parameter to a function. Add that code to top of your code.

local player = Game.GetLocalPlayer()

THANK YOU ,BUT IT STILL CANT BE DONE..

Error running Lua task: [E745F7DC8279E46F] AttachToPlayer:1: Server script attempted to access a client-only field in a namespace. Field: Game.GetLocalPlayer

This can be only used in a client context. If you want to access it from a server script then you can broadcast from client to server or create a player joined event in server. Then you can use it.

okay,thanks for your help~ :grin:

If you tell the details I can help you more.

1 Like

You should probably move this post to #scripting-help instead of Lua snippets - Lua snippets is more intended to be for posts that teach something, not posts that ask something :wink: