Expect string, got instance

I have a script in ServerScriptService than takes in a player and value. The script is below. I want to make it so it increases a players leaderstat by the value. My output in below, any help?

Script:

local event = game.ReplicatedStorage.PlayerClicked

event.OnServerEvent:Connect(function(player, value)
	
	print(player)
	print(value)
	game.Players[player].leaderstats.Clicks.Value = game.Players[player].leaderstats.Clicks.Value + value
	
end)

You already have a reference to your player from the event handler, so you don’t need to access it through Players. Also, because your object reference is a player object and not their name, that’s what you’re getting the error for.

Just replace game.Players[player] with player and you should be good.

On server event gives you the player.

This code gets you the player from their name.

game.Players[playerName]

The error comes from the fact that you are inserting the player in the brackets rather than their name. It would work if you did player.Name, but that would be redundant since you already have player.

So just do this instead.

player.leaderstats.Clicks.Value = playerleaderstats.Clicks.Value + value
1 Like

You have an instance of “player”

game.Players[player].leaderstats.Clicks.Value = game.Players[player].leaderstats.Clicks.Value + value

You are trying to find the player through the PlayerService, so simply request for their name:

game.Players[player.Name].leaderstats.Clicks.Value = game.Players[player.Name].leaderstats.Clicks.Value + value