Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
4

Why does this script only work in studio?

Asked by 9 years ago

This is in a LocalScript in an ImageButton in a ScreenGui in StarterGui.

player = game.Players.LocalPlayer
timer = script.Parent.Timer
enabled = true

function onClick(click)
    if player.ItemCounter["Quick Mushroom"].Value > 0 and enabled == true then
        enabled = false
        player.ItemCounter["Quick Mushroom"].Value = player.ItemCounter["Quick Mushroom"].Value - 1
        player.Character.Humanoid.WalkSpeed = 48
        timer.Visible = true
        timer.Text = "2:00"
        minutes = 2
        for seconds = 120, 0, -1 do
            local seconds2 = seconds - 60
            local secondsdisplay = seconds2
            if seconds == 119 then
                minutes = 1
            end
            if seconds == 59 then
                minutes = 0
            end
            if seconds2 < 10 then
                secondsdisplay = "0"..seconds2
            end
            if seconds < 60 then
                secondsdisplay = seconds
            end
            if seconds < 10 then
                secondsdisplay = "0"..seconds
            end
            if secondsdisplay == 60 then
                secondsdisplay = "00"
            end
            timer.Text = minutes..":"..secondsdisplay
            wait(1)
        end
        player.Character.Humanoid.WalkSpeed = 24
        timer.Visible = false
        enabled = true
    end
end

script.Parent.MouseButton1Click:connect(onClick)

The script runs just fine in studio, but when playing in-game, an error appears on Line 2 stating that "Timer" is not a valid member of ImageButton. However, when going into Studio, there is indeed a TextLabel in the ImageButton named "Timer". What's going on here?

2
len means length. It returns the length of a string. Wow I'm late. EzraNehemiah_TF2 3552 — 9y
0
Haha yeah you are, but I appreciate the answer because I still didn't know what len was until now. IcyArticunoX 355 — 9y

2 answers

Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

The problem is that sometimes the script loads before the actual gui, so 'Timer' may be nil at the moment that your script excecutes.

To fix this, I suggest waiting until 'Timer' has loaded, or check if it has loaded.

Also, your timer could use some touch ups.

local player = game.Players.LocalPlayer
local enabled = true

script.Parent.MouseButton1Click:connect(function()
    script.Parent:WaitForChild("Timer")
    local timer = script.Parent:FindFirstChild("Timer")
    local m = player.ItemCounter["Quick Mushroom"]
    if m.Value > 0 and enabled == true then
        enabled = false
        m.Value = m.Value - 1
        player.Character.Humanoid.WalkSpeed = 48
        timer.Visible = true
        timer.Text = "2:00"
        local minutes = 2
        for i = (minutes * 60), 0, -1 do
            local minutes = math.floor( i / 60)
            local seconds = i - (minutes * 60)
            if tostring(seconds):len() < 2 then
                seconds = tonumber("0"..tostring(seconds))
            end
            timer.Text = tostring(minutes)..":"..tostring(seconds)
         end
        player.Character.Humanoid.WalkSpeed = 24
        timer.Visible = false
        enabled = true
    end
end)
0
This is incredibly helpful. Thanks! (Also, follow-up question: what does "len" mean on line 18?) IcyArticunoX 355 — 9y
Ad
Log in to vote
1
Answered by 9 years ago
timer = script.Parent:WaitForChild("Timer")

Try that. It might not work tho...

Answer this question