How to pick the local player's current leaderstats value with a server-side script?

What do you want to achieve?
I want to pick the local player’s multiplier value in Lua for my Roblox game.
What is the issue?
I have a script that gives clicks to the player based on their multiplier value. However, I don’t know how to pick the local player’s leaderstats value. I need help with this issue.
What solutions have you tried so far?
I have looked for solutions on the Developer Hub, but I couldn’t find any that specifically addressed my issue. I have tried using the PlayerAdded event to get the local player’s multiplier value, but I was not successful.

Here is the script that I am currently using:

local rebirthEvent = game.ReplicatedStorage:WaitForChild("RebirthEvent")
local function giveClicks(plr)
	
	local clicksStat = plr.leaderstats.Clicks
	local multiplier = plr.leaderstats.Multiplier.Value
	if multiplier > 0 then
		clicksStat.Value += 1 * multiplier
	else
		clicksStat.Value += 1
	end
end

game.ReplicatedStorage.GiveClicks.OnServerEvent:Connect(function(plr)
	giveClicks(plr)
end)

game.ReplicatedStorage.GiveAutoClicks.OnServerEvent:Connect(function(plr)
	giveClicks(plr)
end)

Thank you in advance!

This script seems correct. Could you detail what you expect to happen vs what is really happening with your script.

The server doesn’t have a local player, that’s why there is the plr parameter in your functions, it will be which ever client fired the Give[Auto]Clicks event.

My issue is that the script doesn’t seem to be giving the correct number of clicks based on the player’s multiplier value. The clicksStat.Value seems to be adding 1 instead of 1 multiplied by the player’s multiplier value.

Could you show the script that edits the multiplier value? Are you editing this intvalue in a LocalScript or a server Script?

Server-script. And currently I don’t have a script that edits the multiplier value, I’m editing it myself in the explorer when playing the game.

  1. Make sure the MultiplierValue starts as 1 for any new players, so make sure to edit this value for any players who have already played the game, so change their value to also 1.
  2. When they rebirth you want to set their Clicks to 0 (probably) and then add a value to their Multiplier value.
  3. Then each time they click you want to basically do what you did in your example script, except for the rebirth.

So basically:

local function rebirth(player)
   local multiplier = plr.leaderstats.Multiplier
   local current = math.max(1, multiplier.Value) -- gets the highest value
   multiplier.Value = multiplier.Value + 1 -- change this any way you see fit
end

rebirthEvent.OnServerEvent:Connect(rebirth)
1 Like

image

Make sure you are in Server mode when editing that value, if you edit in Client mode then the server will have no idea of the change

1 Like

Oh, makes sense now.

“ServerScriptService.ClickScript:6: attempt to compare number < Instance”

Can you post that script? I don’t see that as an error in your original post.

Weird…

local function giveClicks(plr)
	
	local clicks = plr:WaitForChild("leaderstats").Clicks
	local multiplier = plr:WaitForChild("stats").Multiplier
	if multiplier > 0 then
		clicks.Value += 1 * multiplier
	else
		clicks.Value += 1
	end
end

game.ReplicatedStorage.GiveClicks.OnServerEvent:Connect(function(plr)
	giveClicks(plr)
end)

game.ReplicatedStorage.GiveAutoClicks.OnServerEvent:Connect(function(plr)
	giveClicks(plr)
end)

You’ve changed your script to use :WaitForChild but forgot to get the .Value of your multiplier, either at declaration like it was in the original script, or at the call sites like clicks will work.

Oh, you’re right! Sorry. But then, I get another error…:

“ServerScriptService.ClickScript:7: attempt to perform arithmetic (mul) on number and Instance”

Same deal, needs to be 1 * multiplier.Value

You can tell from the error message because multiplier is and Instance, we want to get it’s number by using .Value

print(typeof(multiplier)) -- Instance
print(typeof(multiplier.Value)) -- number

Yeah, you’re right! The problem is now fixed, thank you!

Also make sure your doing server side checks! So any exploiter wont just give them 1mil + clicks in 1 go

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.