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

i have problem with Touched Event?

Asked by 3 years ago
Edited 3 years ago

I made similar script but with ClickDetector. MouseClick instead of a Touched Event and everything worked well, but what is the problem here I cannot understand

local tool = game.ReplicatedStorage.Key
local giver = script.Parent
local canGive = false

local function GiveTool(player)
    if canGive == false then
        canGive = true
        local clone = tool:Clone()
        clone.Parent = player.Backpack
        wait(1)
        canGive = false
    end
end

giver.Touched:Connect(function(player)
    GiveTool(player)
end)

here is a error:

Backpack is not a valid member of MeshPart "Workspace.OleshaDumayet.LeftHand"

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

on line 05 the function parameter called 'player' is wrong. The parameter in the touch event function is an object that touched the giver.

Here I rewrote your script:

local tool = game.ReplicatedStorage.Key
local giver = script.Parent
local canGive = false

local function GiveTool(otherPart) -- the parameter will always reference to the part that touched the giver no matter what the parameter is called. I call them default parameters
    local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
    if canGive == false then
        canGive = true
        local clone = tool:Clone()
        clone.Parent = player.Backpack
        wait(1)
        canGive = false
    end
end

giver.Touched:Connect(GiveTool)
Ad

Answer this question