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

Why does this script only work in studio?

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 comments

2

len means length. It returns the length of a string. Wow I'm late.

0

Haha yeah you are, but I appreciate the answer because I still didn't know what len was until now.

2 answers

✓ Accepted answer

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)

1 comment

0

This is incredibly helpful. Thanks! (Also, follow-up question: what does "len" mean on line 18?)

timer = script.Parent:WaitForChild("Timer")

Try that. It might not work tho...

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