How to define player in a serverscript?

Hey all!

I put a clickdetector in a part and the script is a normal script with the part as its parent as well, how would I define player to insert a leaderstats value into the player?

local cldet = script.Parent.ClickDetector
local players = game:GetService("Players")
cldet.MouseClick:Connect(function()
	local player = ???
	local ls = player:WaitForChild("leaderstats")
	local clicks = ls:FindFirstChild("Clicks")
	local multiplier = ls:FindFirstChild("multiplier")
	clicks.Value = clicks.Value + (3*multiplier.Value)
	script.Parent:Destroy()
end)

The setup:
image

1 Like

The player that clicks the detector will be in the .MouseClick. put player as the parameter of your function and then create a leaderstat for that player

1 Like

so like this?

local cldet = script.Parent.ClickDetector
local players = game:GetService("Players")
cldet.MouseClick:Connect(function(player)
	local ls = player:WaitForChild("leaderstats")
	local clicks = ls:FindFirstChild("Clicks")
	local multiplier = ls:FindFirstChild("multiplier")
	clicks.Value = clicks.Value + (3*multiplier.Value)
	script.Parent:Destroy()
end)
2 Likes

A click event’s first parameter is the player that activated the click detector.

cldet.MouseClick:Connect(function(player)
    print(player.Name)
end)
2 Likes