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

How to I print my Correct amount of currency in my leaderstats when touhing a part ??

So I'm making a Tycoon game, and I really need help on this bug that I can't Solve:

When I touch a part, I want it to print my CURRENT amount of currency in my leaderstats for testing purposes. And of course I need this to check to see if the player has enough currency to but something in the tycoon

Problem:

When I touch the Part, It prints the correct amount of currency in my leaderstats that I set in this script inside the server script service:

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = 'leaderstats'
	
	local currency = Instance.new("IntValue", leaderstats)
	currency.Name = 'Cash'
	currency.Value = 150 -- it prints THIS ammount here
end)

But when I CHANGE my Currency Value to something different (inside the explorer tab and in my leaderstats) and touch the part AGAIN, it will still print the same value as it did before (150) No matter how many times I change it, it prints the same value over and over again.

Also, when I play the game again, I DON'T touch the part and change the currency value in my leaderstats, and then when I touch it, it will still print the same value I set in the script above.

Here is my script for when I touch the part:

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		local playerLeaderstats = player:FindFirstChild('leaderstats')
		if playerLeaderstats then
			print(playerLeaderstats.Cash.Value)
		end
	end
end)

I thought this code will update every time I touch it but it doesn't

If you can suggest why it is not working or even a completely different method on how to do it, I will greatly appreciate it. This bug is delaying my development of my game, and again, if you know what is wrong or have a suggestion regarding a different message please let me know ASP.

Thanks for reading this

2 comments

0

You need to make sure the value is changed on the server, not the client.

0

Try changing the value using /console instead of explorer or by using admin commands.

1 answer

✓ Accepted answer

I might know the issue.

I am assuming these two scripts are server scripts. They are written correctly, it's just you're changing them wrong.

In the Home tab, there should be a button that says Current: Client. Click on this and it should change to Current: Server. Now try changing the value, and switching it back to Client. Step on the part, and see if it prints the correct value.

Hope this helped!

1 comment

0

@ TheMinecraftSmarts, Unfortunately I just figured out what was wrong myself, and it worked when I published it and tested it there. But you where on the right lines and If I didn't manage to figure it out myself, you feedback would of been very helpful. But thanks anyway for the answer :)

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