How can I fix this Coin GUI Script

Sorry it took so long to respond, I’ve been busy doing something else!

Is the problem inside this part?

local Player = game.Players.LocalPlayer
script.Parent.Visible = true 
local Stats = Player:WaitForChild("leaderstats")
local Cash = Stats:WaitForChild ("Coins")
repeat
	wait(0.01)
	script.Parent.Text =  (format(Cash.Value)),"Out","Linear"
	
until script.Disabled == true

I see to have an error thingy on the (I assume this means nothing)

Script.Parent.Text = (format(Cash.Value)),"Out","Linear"

It basically means you are trying to set script.Parent’s text to 3 values: format(Cash.Value), “Out”, and “Linear”. Try putting the 3 of them in parentheses.

Something like this?

local Cash = Stats:WaitForChild ("Coins")
repeat
	wait(0.01)
	script.Parent.Text =  (format(Cash.Value))("Out","Linear")
	
until script.Disabled == true
1 Like

Instead of using cash.Changed, you should just have a loop instead. Also, when you sell the coin, your game.Players.LocalPlayer.leaderstats.Coins.Value turns to 0 but the variable dif does not since dif is larger than game.Players.LocalPlayer.leaderstats.Coins.Value. So, this script will definitely work.

local dif = game.Players.LocalPlayer.leaderstats.Coins.Value
while wait() do
	if dif ~= game.Players.LocalPlayer.leaderstats.Coins.Value then
        local dif2 = game.Players.LocalPlayer.leaderstats.Coins.Value - dif
        dif = game.Players.LocalPlayer.leaderstats.Coins.Value
	    if dif2 > 0 then
            local random = math.random(1, 900)
	    	local xnew = random / 1000
	    	local new = script.Coins:Clone()
	    	newCoinsInfo.Text = "+".. game.Players.LocalPlayer.leaderstats.Coins.Value - dif
	    	new.Position = UDim2.new(xnew, 0, 1, 0)
	    	new.Parent = script.Parent
	    	wait(0.1)
	    	new:TweenPosition(UDim2.new(new.Position.X, 0, -0.1, 0))
	    	wait(2)
	    	new.Parent:Destroy()
        end
	end
end

If it worked, it would be appreciated to have this set as solution :slight_smile: Have a great day/night!

Do I need to change anything inside the script? Or leave it as it is.

You should change this local script:

local Players = game:GetService("Players")
local plr = script.Parent.Parent.Parent
local leaderstats = plr:WaitForChild("leaderstats")
local cash = leaderstats:WaitForChild("Coins")

local dif = 0
delay(.25, function()
    dif = cash.Value
end)

cash.Changed:Connect(function()
	if dif ~= cash.Value and dif <= cash.Value then
		local random = math.random(1, 900)
		local xnew = random / 1000
		local new = script:WaitForChild("Coins"):Clone()
		new:WaitForChild("CoinsInfo").Text = "+".. cash.Value - dif
		new.Position = UDim2.new(xnew, 0, 1, 0)
		new.Parent = script.Parent
		dif = cash.Value
		wait(0.1)
		new:TweenPosition(UDim2.new(new.Position.X, 0, -0.1, 0))
		wait(2)
		new.Parent = nil
	end
end)

Into this local script:

local dif = game.Players.LocalPlayer.leaderstats.Coins.Value
while wait() do
	if dif ~= game.Players.LocalPlayer.leaderstats.Coins.Value then
        local dif2 = game.Players.LocalPlayer.leaderstats.Coins.Value - dif
        dif = game.Players.LocalPlayer.leaderstats.Coins.Value
	    if dif2 > 0 and script:FindFirstChild("Coins") then
            local random = math.random(1, 900)
	    	local xnew = random / 1000
	    	local new = script.Coins:Clone()
	    	new.CoinsInfo.Text = "+".. game.Players.LocalPlayer.leaderstats.Coins.Value - dif
	    	new.Position = UDim2.new(xnew, 0, 1, 0)
	    	new.Parent = script.Parent
	    	wait(0.1)
	    	new:TweenPosition(UDim2.new(new.Position.X, 0, -0.1, 0))
	    	wait(2)
	    	new.Parent:Destroy()
        end
	end
end

If it worked, it would be appreciated to have this set as solution :slight_smile: Have a great day/night!

this would work but i would also recommend using task.wait() because it is way more efficient than wait()

This doesn’t seem to work? I am getting this error. But Roblox is breaking right now, so none of my data stuff seems to work currently and I am getting aton of errors. But besides that, I get this error.

Right now, roblox seems to have an issue. You can’t log in if you log out, and if you go to the roblox’s page, you will see an error message. We have to wait for roblox to fix this problem.

Alright, well Ima head to bed and we’ll see in the morning. Roblox is having terrible issues right now for the past few hours, so I will wait till tomorrow just to see if it works! I’ll make sure to keep you updated!

Thank you! ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎

Ok ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎ ‎‎‎

So i just edited it. Will work now

Sorry, in my first post, I was talking about the second script instead of the first. But in the script that @Spellwastaken provided, there’s a few problems:

  1. Using a forever wait() loop is always a bad choice. This is very performance costly and I don’t see why you would use it over a .Changed connection, which would only fire when the Coins value CHANGE.
  2. In Line 10, I think you accidentally delete the period between “new” and “CoinsInfo”.

Here just change your local script into this:

local Players = game:GetService("Players")
local plr = script.Parent.Parent.Parent
local leaderstats = plr:WaitForChild("leaderstats")
local cash = leaderstats:WaitForChild("Coins")
local gui = script:WaitForChild("Coins")

local dif = 0
delay(.25, function()
    dif = cash.Value
end)

cash.Changed:Connect(function()
	if dif < cash.Value then
		local random = math.random(1, 900)
		local xnew = random / 1000
		local new = gui:Clone()
		new:WaitForChild("CoinsInfo").Text = "+".. cash.Value - dif
		new.Position = UDim2.new(xnew, 0, 1, 0)
		new.Parent = script.Parent
		dif = cash.Value
		wait(0.1)
		new:TweenPosition(UDim2.new(new.Position.X, 0, -0.1, 0))
		wait(2)
		new.Parent = nil
	else
		dif = cash.Value
	end
end)
1 Like

Thank you, it worked! I really do appreciate it!

If there are more than one correct answers, you should choose the first one because the people that replied after could be copying the first person. It would be unfair for others. Please change your solution mark.

Thank you for setting the solution correctly

I don’t mind if you are marked as solution, but just so you know, I actually already identified the problem in my first post, which is explaining how the reason that the OP’s script did not work was due to not updating the “dif” variable when the coin value is set to 0:

You basically restated the same problem in your first post:

And in addition, you recommended the OP to use wait() instead of using events. The problem with wait() in this scenario is that:

  1. If wait() is running smoothly, you’re running the code at a rate of about 10 times a second
  2. wait() could be laggy and wait up to seconds
  3. The code wouldn’t immediately run after the coin value changes

There’s a reason why events exist and please use them if applicable, which in this case it is. I recommend you look at this post: Avoiding wait() and why. Finally, sorry if this offends the OP, but it appears as if the OP is only seeking for spoon-fed code instead of trying to solve the problem themselves.

1 Like

For the wait(), I recommend using it as events are unreliable. Sometimes the client doesn’t receive it because of internet instability and therefore the client would not be able to update it.

You COULD use RunService.RenderStepped:Wait() or task.wait(), both of which are more efficient than wait(). Also, you could always just handle the event on the server and fire a remote event.

If the client has internet instability, I don’t think we can do anything about it. :frowning: