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

How do I find the player who clicked?

So I want it so when a player clicks a button it changes the color of a tool in his inventory. I can do that but how do I find the player who clicked? (clickdetector)

3 comments

0

Can you add more info, what type of button are you useing?

0

Clickdetector

0

Just wanted to add that a click detector will not work with filtering enabled.

1 answer

A quick check of the wiki turned up this. Basically, the MouseClick event knows the player that clicked it and we can use that.

--Script is assumed to be placed inside the Part the ClickDetector resides in.

function onClicked(player)
	print("Button clicked by " .. player.Name);
	--Insert code to adjust the player's tool

	--Edit: A demonstration of how to do stuff with the player's character, as shown by the example of adding Sparkles to the character's torso.
	local torso = player.Character:findFirstChild("Torso")
	if torso == nil then return end
	local spark = Instance.new("Sparkles")
	spark.SparkleColor = Color3.new(1, 0, 0)
	spark.Parent = torso
	wait(2)
	spark:Destroy()
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

2 comments

0

How do I find the player? like what do I do to find the player in the workspace

0

Right now, "player" refers to the object in the Players list.  If you want their character in the Workspace, you could get the player's character model with "player.Character" -- I'll edit in a bit more code to show how you might do stuff with that.

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