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

How do I find the players character in Workplace and the player in Players for a onTouch part?

Asked by 9 years ago

I have a part and I want it when a player touches It, It gives them a tool and takes away their health. So I need access to their character in Workspace defined as a variable and to their Backpack.

function Touch(player)
    local player = game.Players:FindFirstChild("player")
    local character = game.Workspace:FindFirstChild("player")
    game.ServerStorage.Tool:Clone().Parent = player.Backpack
    character.Health = character.Health - 50
end

script.Parent.Touched:connect(Touch)

1 answer

Log in to vote
0
Answered by
Udrii 0
9 years ago
function Touch(part)
    local character = part.Parent
    if game.Players:GetPlayerFromCharacter(character) then
        local player = game.Players:FindFirstChild(character.Name)
        game.ServerStorage.Tool:Clone().Parent = player.Backpack
        character.Humanoid.Health = character.Humanoid.Health - 50
    end
end

script.Parent.Touched:connect(Touch) -- Guessing script in part
Ad

Answer this question