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

Cash value fails to update?

I'm making a dialog shop. It gives me the bread, but the value of 'cash' won't change.

local cash = game.StarterGui.ScreenGui.cash.cashv
local merchantdialouge = workspace.Merchant.Head.Dialog:GetChildren()
local Bread = game.ServerStorage.Bread
local dialog = script.Parent

dialog.DialogChoiceSelected:connect(function(player, choice)
	if choice == script.Parent.buybread then
    	if cash.Value >= 5 then -- 5 is the amount of gold you need to purchase bread
       	game.ServerStorage.Bread:clone().Parent = player.Backpack
       	cash.Value = cash.Value - 5 -- subtract the amount of cash you need to purchase
   		end
	end
end)

Value script:

local cashdisplay = script.Parent
local cashvalue = script.Parent.cashv.Value

cashdisplay.Text = "$"..cashvalue..""

2 answers

✓ Accepted answer
local cash = game.StarterGui.ScreenGui.cash.cashv

The issue is probably there. Correct me if I am wrong, but you are trying to update the value of a textlabel inside the PlayerGUI. What you've done wrong there is update it inside the StarterGUI, which is what players spawn with when they join the game.

EDIT: Didn't realise you could edit an answer, but oh well here's the instructions for fixing your issues.

Change the name of the value in the TextLabel to ' cashv ' (excluding the quotation marks) That was one factor in why it didn't work for you.

Ensure there is bread in ServerStorage Because the bread wasn't in ServerStorage, the script stopped there and didn't continue after that. I managed to avoid this by making the lines with the bread into comments.

Replace the script inside the dialog of the merchant to this: The only change are is in Line 8 where I moved the local for the variable into the event caller, and where I made it so that it would change for the player rather than inside the StarterGUI.

local merchantdialouge = workspace.Merchant.Head.Dialog:GetChildren()
local Bread = game.ServerStorage.Bread
local dialog = script.Parent
 

dialog.DialogChoiceSelected:connect(function(player, choice)
    if choice == script.Parent.buybread then
		local cash = player.PlayerGui.ScreenGui.TextLabel.cashv
        if cash.Value >= 5 then -- 5 is the amount of gold you need to purchase bread		
	        game.ServerStorage.Bread:clone().Parent = player.Backpack
	        cash.Value = cash.Value - 5 -- subtract the amount of cash you need to purchase
        end
    end
end)

Finally, delete the LocalScript inside the TextLabel and replace it with a script. Not much difference, but here is the script if you need it:

while true do
	wait() -- prevents crashes
	local cash = script.Parent.cashv
	local cashDisp = script.Parent
	cashDisp.Text = "$"..cash.Value -- sets it
end

That's all, I'd appreciate it if you accepted the answer. Any issues, feel free to ask! :)

3 comments

0

Yes, I think that's what I did.

0

@puppylover133 Revised, please read the answer again.

0

Thanks, that fixed it! :)

I believe the problem lies in the Value script. You would need to put it in a loop; that way, instead of updating the display just once, it will constantly update it. Here is the complete code:

local cashdisplay = script.Parent
local cashvalue = script.Parent.cashv.Value

while true do
	cashdisplay.Text = "$"..cashvalue
	wait()
end

2 comments

0

Still won't update.. the place is here: http://www.roblox.com/games/290394105/tests if anyone wants to look at it. I uncopylocked it too.

0

Making variables equal to properties won't work, because the variable won't change if the property changes later. Also, a Changed event should be used instead of a loop. Can't type an answer right now tho :(

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