Stats handler not working!

So I made this stat handler for the client but its not working idk why it works for every other thing, know I dint name anything wrong I prnted it it should all be right als in the players stats all the names are right. Here’s the part of my script:

statsEvent.OnServerEvent:Connect(function(player, amount, symbol, stat)
	print(amount, symbol, stat)
	if symbol == "+" then
		player.Values[stat].Value += amount
	elseif symbol == "-" then
		player.Values[stat].Value -= amount
	elseif symbol == "*" then
		player.Values[stat].Value *= amount
	elseif symbol == "/" then
		player.Values[stat].Value /= amount
	elseif symbol == "=" then
		player.Values[stat].Value = amount --- this line is error
	end
end)

I get this error on the line I marked: invalid argument #2 (string expected, got Instance)

1 Like

The error means stat is an Instance, and it expected a string, so you’ll need to use it’s name to access the value like stat.Name

2 Likes

but it was a string when I printed it and it always was?

1 Like

Try to print type(stat) and check if it’s actually a string, it’s the only thing that can go wrong

1 Like

yes it prints a string I’m 100 sure

1 Like

Isn’t the 2nd argument “symbol”? It’s probably the symbol variable causing issues.

1 Like

I printed them all they where al correct

1 Like

Did you try replacing stat with stat.Name?

1 Like

Give this a go?

statsEvent.OnServerEvent:Connect(function(player, amount, symbol, stat)
	if typeof(amount) == 'Instance' then
		warn('You sent an Instance as the argument for amount')
		if pcall(function() return amount.Value end) then
			amount = amount.Value
		else
			warn('Cannot Fix this, please put in a valid value')
			return
		end
	end


	if symbol == "+" then
		player.Values[stat].Value += amount
	elseif symbol == "-" then
		player.Values[stat].Value -= amount
	elseif symbol == "*" then
		player.Values[stat].Value *= amount
	elseif symbol == "/" then
		player.Values[stat].Value /= amount
	elseif symbol == "=" then
		player.Values[stat].Value = amount 
	end
end)

But also, realistically I wouldn’t have an event like this, because a hacker can have complete control over their stats with something like this.

2 Likes

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