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

OnTouched function script don't kill and add points automatically sometimes how to fix?

Asked by 5 years ago
Edited 5 years ago

So Basiaclly I made this script so when ever you touch a part called Touched It gives your team 1 point and kills the player but the thing is sometimes its adding points automaticlly and also It's not killing the player please make it verify that the player who touched it has humanoid I forgot how to

game.Workspace.Touch.Touched:Connect(function(player)

if player.Touched then

game.Workspace.RedPoint.Value = game.Workspace.RedPoint.Value + 1

player.character.Humanoid.Health = 0

end

end)

0
Touched does not pass the player as its argument. It passes the part that touched the part firing the event as its argument. DeceptiveCaster 3761 — 5y
0
You could do if player.Parent:FindFirstChild("Humanoid") do ... the res of your code, but the player value you are passing in the function is really the part. You could pass the value 'player' but it's still reference the part touched,not the actual player as MCAndRobloxUnited stated. uhohspaghettiy0o 15 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

The only reason why your script isn't working is because you're passing the wrong parameter to Touched's argument. The argument that Touched passes is the part that touched the part that is firing the Touched event.

Here's a simple fix:

local function AddAndKill(part)
    local player = game:GetService("Players"):GetPlayerFromCharacter(part.Parent) -- This is the player
    if player then
        workspace.RedPoint.Value = workspace.RedPoint.Value + 1
        player.Character.Humanoid.Health = 0
    end
end
workspace.Touch.Touched:Connect(AddAndKill)
Ad

Answer this question