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

How do you check the name of the part another part touched?

Asked by
DemGame 271 Moderation Voter
3 years ago

This is my script so far; it is set as the child of a hitbox:

local debounce = false

script.Parent.Touched:connect(function(hit)
    if debounce == false then
        print("1")
        if not script.Parent.Name == hit.Parent.Name then
            print("2")
            if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
                print("3")
                debounce = true
                hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 10
                hit.Parent.Humanoid.WalkSpeed = 8
                wait(1)
                hit.Parent.Humanoid.WalkSpeed = 16
                debounce = false
            end
        end
    end
end)

In line 6, I try to check if the script's parent's name is equal to the part it hit's name, but i think i did "hit.Parent.Name then" wrong. Any suggestions?

1 answer

Log in to vote
2
Answered by 3 years ago

It looks like the checks are to make sure that it's a player you're touching. In that case, try this:

local debounce = false

script.Parent.Touched:Connect(function(hit)
    if not debounce then -- make sure debounce is set to false
        debounce = true
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if player then -- make sure we don't work with a nil value, so the next part can only be performed when it's a player we are touching
            print(player.Name) -- This will print the player's name
            local character = player.Character -- this is the player's avatar in Workspace

            character.Humanoid.Health -= 10 -- take away 10 from health
            character.Humanoid.WalkSpeed = 8
            wait(1)
            character.Humanoid.WalkSpeed = 16

            debounce = false
        end
    end
end)
0
Thanks. DemGame 271 — 3y
Ad

Answer this question