I want to make a Part that when I click on it, gives me a Sword. I'm using a normal script.
I receive this error when I play my game in Studio:
clickDetector = script.Parent giver = clickDetector.Parent sword = game.ServerStorage.ClassicSword:Clone() function giveSwordToPlayer() local player = game.Players.LocalPlayer local playerBackpack = player.Backpack sword.Parent = playerBackpack end clickDetector.MouseClick:Connect(giveSwordToPlayer())
Also if anyone has any valuable sources to expand my knowledge on Rbx.lua other than the new Robloxdev wiki, please let me know.
Thank you
LocalPlayer
is nil on the server. It can only be used on the client. Luckily, the MouseClick
event passes the player as a parameter.local clickDetector = script.Parent local giver = clickDetector.Parent local sword = game.ServerStorage.ClassicSword:Clone() local function giveSwordToPlayer(player) local playerBackpack = player.Backpack sword.Parent = playerBackpack end clickDetector.MouseClick:Connect(giveSwordToPlayer) -- no extra brackets