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

Help with player-player collisions?

function touched(body)
	body.HumanoidRootPart.Touched:Connect(function(hit)
		local otherPlayer = game.Players[hit.Parent.Name]
		print(otherPlayer)
		if otherPlayer == game.ServerScriptService.RoundHandler.EnemyPlayerName.Value then
			script.Parent.Humanoid.Health = 0
		end
	end)
end
game.Players.PlayerAdded:Connect(function(newPlayer)
	newPlayer.CharacterAdded:Connect(touched)
end)

the player is supposed to be killed when the player with enemyplayername name touches the person. the script doesn't work.

1 answer

✓ Accepted answer

You're comparing an instance of a player to a string that supposedly contains their name.

So, instead of doing this:

if otherPlayer == EnemyPlayerName.Value then
	script.Parent.Humanoid.Health = 0
end

You do this:

if otherPlayer.Name == EnemyPlayerName.Value then
	script.Parent.Humanoid.Health = 0
end

Think of it like the concept of like terms in math. You can't compare unlike terms in math, just as you can't compare unlike data types in computer programming.

4 comments

0

I'm also kind of confused as to what your script is actually doing. It looks like it's inside of your character, and whenever someone else touches the enemy player, you die? Wouldn't you tailor it to kill you when you touch the enemy player and not when someone else touches the enemy player? Just a little confused is all.

0

Sorry, When i touch the enemy player I'm supposed to die.

0

and also game.Players[hit.Parent.Name] <-- the string is right there, it's getting the name

0

No. It's not getting the name. It's getting the instance of the player that has the name you provided.

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