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

Change SurfaceGui text to something else via Scripting?

I want to have a chest that, when clicked, gives the player an item. Then, the Chest will cooldown for 45 seconds until it can be used again. All the code for this part works.

My problem is in the sign displayed above the chest. It says "CLICK FOR TREASURE!", but once the chest is clicked, it will change to "COOLING DOWN...". This part doesn't work.

Code:

local tool = script.Parent.Item.Value
local signText = script.Parent.PizzaSwordSign.SurfaceGui.TextLabel.Text
local takable = true
local Sound = game.Workspace.Map.Maze.PizzaSwordGiver.Script.Sound

local function give(playerWhoClicked)
    if takable then
        local sword = game.ServerStorage["PizzaSword"]:Clone()
        sword.Parent = playerWhoClicked.Backpack
        sword:Clone().Parent = playerWhoClicked.StarterGear
        Sound:Play()
        takable = false
        signText = "COOLING DOWN..."
        wait (45)
        takable = true
        signText = "CLICK FOR TREASURE!"
    end
end

script.Parent.ClickDetector.MouseClick:Connect(give)

Thanks in advance, Mex

1 answer

✓ Accepted answer

###The problem lies in the fact that you wrongly assume that the signText variable is a reference to the actual property of the TextLabel. If the text of the TextLabel changes, the variables you have defined won't update; only its Text property will change. In order to fix this, you can remove the signText variable and only make direct accesses to the Text property of the TextLabel. You could also modify the variables to hold the TextLabel object itself. Same goes for the tool variable.

local tool = script.Parent.Item
local signText = script.Parent.PizzaSwordSign.SurfaceGui.TextLabel
local takable = true
local Sound = game.Workspace.Map.Maze.PizzaSwordGiver.Script.Sound

local function give(playerWhoClicked)
    if takable then
        local sword = game.ServerStorage["PizzaSword"]:Clone()
        sword.Parent = playerWhoClicked.Backpack
        sword:Clone().Parent = playerWhoClicked.StarterGear
        Sound:Play()
        takable = false
        signText.Text = "COOLING DOWN..."
        wait (45)
        takable = true
        signText.Text = "CLICK FOR TREASURE!"
    end
end

script.Parent.ClickDetector.MouseClick:Connect(give)

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