Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does this not put the wand in my backpack?

Asked by 9 years ago

This is inside a part in Workspace.

local part = script.Parent

function onTouch(player) 
local wand = game.ReplicatedStorage.Tool:Clone()
wand.Parent = player.Backpack
end


script.Parent.Touched:connect(onTouch)

I get this error Backpack is not a valid member of Part

1 answer

Log in to vote
1
Answered by 9 years ago

The Touch Parameter returns your the object that triggered the event to access the Player you'll need to use GetPlayerFromCharacter()

local part = script.Parent

function onTouch(player) 
local wand = game.ReplicatedStorage.Tool:Clone()
local Player = game.Players:GetPlayerFromCharacter(player.Parent)--Getting the Player using the Character.
if Player then--This prevent errors from occurring, like for example if your function was triggered by an object it won't have a Backpack would it?
wand.Parent = Player.Backpack
    end
end


script.Parent.Touched:connect(onTouch)

~UserOnly16Charcters, Hoped I helped you to answer your question! If you have any further question, don't hesitate to comment below!!

Ad

Answer this question