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

How to detect if the touched object is the same as the localplayer?

Hello, thanks for your coming. I wonder how to detect if the touched object is the a localplayer? Here is my code:

game.Players.PlayerAdded:connect(function(localplayer)
	spawn = false
	script.Parent.Touched:connect(function(hit)
		if hit.Parent:FindFirstChild(“humanoid”) then
-- how to add a script here to detect if it is the local player?
			spawn = true
		end
	end)
	player.CharacterAdded:connect(function(respawn)
		if spawn then
			print(“success!”)
		end
	end)
end)

If you know then please tell me, thank you!

1 answer

✓ Accepted answer

Hi there! This is more simple than you may think. What you need to do, is take the players string and check the "it" value to see if it matches the characters name, and exempt it if it does.

Here is a polished version of your code, but the main takeaway here should be that when calling the PlayerAdded() function the "localplayer" is stored as a Player value and has a name value you can use along with the character name, as the two name values are the same unless changed.

game.Players.PlayerAdded:connect(function(Player)
    spawn = false
    script.Parent.Touched:connect(function(Hit)
        if Hit.Parent:FindFirstChild(“Humanoid”) then
            if Hit.Parent.Name == Player.Name then
                --code for if it is the localplayer
            else
                --code for if it isn't the localplayer
            end
        end
    end)
    player.CharacterAdded:connect(function(respawn)
        if spawn then
            print(“success!”)
        end
    end)
end)

Happy coding, Raie.

3 comments

0

You already made that in the code line 5. Doesn't it work? If not maybe you should remove .Name idk.

0

Thank you so much! But I just got 1 more question, Y it does not work when I tried to remove the .Name function? Should not it be the same thing? Cuz I guess that the parent of the hit function is the player, they mean the same thing.

0

Ps: Thank you again, but for saving my life. Cuz I promised to my friend to complete his games code and if I do not complete it in this weekend my friend is going to slaughter me and I will leave this beautiful world forever. XD

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