Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

This script does not limit my currency? Help would be appreciated!

Hey!

Thanks for taking your time out of your day to help me.

As the title states, I got an issue limiting the amount of money a player can hold.

So what I'm trying to do is make the player only hold 100 money at once, and when the player tries to get more, his money gets limited back to 100 . This also is whenever the player loses money, he cannot go less than 0 .

This script does not limit anything though! The player's money goes less than 0 and more than 100 !

Any help would be surely appreciated.

I'm new to coding, so all this is new to me.

Code:

wait (20)

game.Players.PlayerAdded:Connect(function(player)
	
	if player.leaderstats2.Money.Value <= 0 then
		player.leaderstats2.Money.Value = 0
		end
	if player.leaderstats2.Money2.Value <= 0 then
		player.leaderstats2.Money2.Value = 0 
	end
	if player.leaderstats2.Money.Value <= 100 then
		player.leaderstats2.Money.Value = 100
		end
	if player.leaderstats2.Money2.Value <= 100 then
		player.leaderstats2.Money2.Value = 100
	end
	
	
end)


2 comments

0

Did you comment on my answer?

0

Yeah, I still can't get it to work. Still goes below.

1 answer

Hi, the problem is that the script only runs once; when a player joins. What you need is something that waits for changes in the amount of money and changes it.

To do that you must use the :GetPropertyChangedSignal(). This works for any value that you are looking for.

game.Players.PlayerAdded:Connect(function(player)
	player.leaderStates.Money:GetPropertyChangedSignal("Value"):Connect(function()
		-- do stuff
	end)
end)

Beyond that, what you're doing is a waste of lines since you can just condense it to math.clamp(x, y, z) which prevents number x from being below y and above z. In your case it would be: math.clamp(Money.Value, 0, 100)

Additionally, if you see yourself using a specific value or path (you've used player.leaderstats2 eight times) Substitute it with a local variable.

3 comments

0

Doesn't work. Or I can't get it to work anyways.

0

I have a script that deducts a dollar each few seconds that runs for the whole time a player is on the server.

0

Does it give an error? Is this in a local script? Are you just copying the code in my answer?

This discussion is preserved as it was. New answers, comments, and votes are closed.