Why isn't this script working?

This script is for when the click detector is activated the player gets his team changed.

This is a server script in workspace

Line of code which is wrong, trying to get the player

local player = OnClicked.Parent

This is the error that comes up 16:45:36.670 - Workspace.Parttouchcick1.Script:5: attempt to index function with ‘Parent’

local Clicked = false

function OnClicked()
if Clicked == false then
		local player = OnClicked.Parent
		player.leaderstats1.Exp = player.leaderstats1.Exp + 10
		player.Team = game.Teams.Citizen
		
Clicked = true
elseif Clicked == true then 
	
Clicked = false
end
end
script.Parent.ClickDetector.MouseClick:Connect(OnClicked)

OnClicked is a function, Functions do not have a parent.
However in this line you can pass in this
function OnClicked(PlayerClicked)
and this will be the Player who clicked it :slight_smile:

function OnClicked(Player)
if Clicked == false then
		Player.leaderstats1.Exp = Player.leaderstats1.Exp + 10
		Player.Team = game.Teams.Citizen
Clicked = true
elseif Clicked == true then 
	
Clicked = false
end
end
script.Parent.ClickDetector.MouseClick:Connect(OnClicked)
1 Like

I want to get the player not the part

Also are you aware your way of doing a Debounce will only work for one player?

game.Players:GetPlayerFromCharacter(part.Parent)

Will give you the player instead of the character’s part

1 Like

No reason to do this as the ClickDetector returns the Player who clicked it.

MouseClick

Also I edited my response above.

What exactly is your use case? That would help significantly in suggesting a more optimal solution.

1 Like