Search code examples
luarobloxroblox-studio

Variable not updating in Roblox Studio / Lua


I'm trying to make a system in roblox studio, in which whenever a tool is activated it will play a animation and make an variable which will be the amount of gems, and then that variable will be put into the PlayerGUI but the variable is not being updated and it is being stuck on 1.This is the code I wrote -

local tool = script.Parent
local anim = tool:WaitForChild("1")
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local loadanim = humanoid:LoadAnimation(anim)
local PlayerModule =       require(game.Players.LocalPlayer.PlayerScripts:WaitForChild("PlayerModule"))
local Controls = PlayerModule:GetControls()
local clicks = 0
local c = true
local confirm = false
local confirm2 = false
local confirm3 = false
local confirm4 = false
local GUI = player.PlayerGui.ScreenGui
local gems= GUI.TextLabel
local amount = 0
local times = false
local how = 0

tool.Activated:Connect(function ()
amount = amount + 1
gems.Text = tostring(amount)
loadanim:Play()
script.Disabled = true
Controls:Disable()
wait(1.5)
Controls:Enable()
wait(.2)
script.Disabled = false
clicks = 1
confirm = true
end)

For your information - The variable Amount is the amount of gems and the variable Gems is the TextLabel so I can change the text. And I also printed the variable multiply times and it still showed '1' so there is no problem with the Gems TextLabel.

I tried making a another variable and updating it to a special value so whenever the gems amount is still 1 even though activated twice, but it was still not working.I don't know how?!


Solution

  • local scriptDisabled
    tool.Activated:Connect(function () if scriptDisabled then return end
    amount = amount + 1
    gems.Text = tostring(amount)
    loadanim:Play()
    scriptDisabled = true
    Controls:Disable()
    wait(1.5)
    Controls:Enable()
    wait(.2)
    scriptDisabled = false
    clicks = 1
    confirm = true
    end)