Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

2 comments

0

Touched does not pass the player as its argument. It passes the part that touched the part firing the event as its argument.

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.

1 answer

✓ Accepted answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.