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

Need help touching up this script, how to get it to find out which player touched it?

Basically, I need a script to find out which player touched a block.

This is what I have, not sure if I'm on the right path or not?


Brick = script.Parent
Busy = false 

function Touched(Part)
	if not Busy then
		Busy = true
	if Part.Parent:findFirstChild("Humanoid") then 
			LocalPlayer = Part.Parent:findFirstChild("Humanoid") 
		end
	end
end

script.Parent.Touched:connect(function(hit)
	script.Parent.BrickColor = 
BrickColor.new(game.Players.PowerHouse2424.TeamColor.Color)

end)

1 answer

✓ Accepted answer

You can use :GetPlayerFromCharacter() to get the player that touched the part.

Here’s an example :

local part = script.Parent

part.Touched:Connect(function(hit)
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent) -- :GetPlayerFromCharacter() uses the player’s character/model to get the player
    if plr then -- Checks if the player is actually a player to prevent errors
        print(plr.Name) -- Prints player’s name
    end
end)

You can use this example to understand :GetPlayerFromCharacter() and use it in your script.

Hoped this helped!

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