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

Making sure it's only a player touching?

Asked by
NotSoNorm 777 Moderation Voter
9 years ago

I have this script, and it works fine (for the most part) but when I touch it, it senses the hats also, how would I make it only take the characters name?

function onTouch(hit)
    if game.Workspace.Positions.First.Value == "" then
game.Workspace.Positions.First.Value = hit.Parent.Name
    elseif game.Workspace.Positions.Second.Value == "" then
        game.Workspace.Positions.Second.Value = hit.Parent.Name
            elseif game.Workspace.Positions.Third.Value == "" then
        game.Workspace.Positions.Third.Value = hit.Parent.Name
        wait(1)
        game.Workspace.Positions.Last.Value = true
end
end
script.Parent.Touched:connect(onTouch)

1 answer

Log in to vote
2
Answered by 9 years ago

Very simple solution, an if statement checking if the touch part's classname is not a hat, and it's parent has a humanoid (Just to make sure it's a character touching)

It could be done as follows:

function onTouch(hit)
    if not hit:IsA("Hat") and hit.Parent:findFirstChild("Humanoid") then
        if game.Workspace.Positions.First.Value == "" then
            game.Workspace.Positions.First.Value = hit.Parent.Name
        elseif game.Workspace.Positions.Second.Value == "" then
            game.Workspace.Positions.Second.Value = hit.Parent.Name
        elseif game.Workspace.Positions.Third.Value == "" then
            game.Workspace.Positions.Third.Value = hit.Parent.Name
            wait(1)
            game.Workspace.Positions.Last.Value = true
        end
    end
end
script.Parent.Touched:connect(onTouch)

Hope it helps!

0
Thanks! NotSoNorm 777 — 9y
0
The Hat check isn't needed. Hat's don't have Humanoids. Perci1 4988 — 9y
1
... I did the hat check so if the part is touched by a hat, it does not continue, because that's what he asked, and for the humanoid check, I am checking if the parent has a humanoid, not if hat has the humanoid, if I don't put the hat check, it will work if it's touched by a hat, which he doesn't want. AbsoluteAxiom 175 — 9y
Ad

Answer this question