Scripting Problem Help Needed!

Hello while making a script I noticed a problem. Basically my problem is when a user clicks a button and leaves the code inside the button breaks at a certain point. Then I noticed I had an error that only happened when a user clicked the button and left. Here is the code:

ClickButton = game.Workspace.Button

ExitPart = game.Workspace.EXITPART

local Active = true

local Players = game:GetService(“Players”)

local Outcomes = { “Obby1”, “Obby2”, “Obby3”, “Obby4”, “Obby5”, “Obby6”, “Obby7”, “Obby8”, “Obby9”, “Obby10”, “Obby11”, “Obby12”}

ClickButton.ClickDetector.MouseClick:Connect(function(player)
local GUI = player:FindFirstChild(“PlayerGui”)
local Timer = GUI:FindFirstChild(“TimerGUI”)

--if on
if Active == true then
	print("Button on")
	local Obby = game:GetService("ServerStorage")[Outcomes[math.random(1, #Outcomes)]]:Clone()
	Obby.Parent = workspace
	

	
	Active = false

-------------timer--------------------------------------------------------------------------------

	local timer = game.ReplicatedStorage.Timer

	local lengthTime = 51
	
	repeat
	for i = lengthTime, 0, -1 do
		timer.Value = i
		wait(1)
	end
	until timer.Value == 0
	
	--TIMER RUNS OUT 
	Obby:Destroy()
	---Error here
		Timer.TimerText.Text = "Game ended"
		ExitPart.CanCollide = false
		wait(3)
		Timer.TimerText.Text = ""
		ExitPart.CanCollide = true
		Active = true
		end

–If button is off
if Active == false then
print(“Button off”)
end
end)

Extra INFO: image

Error picture:

Sincerely, DevBattery

Thank you for your time
NO BLOCK CODE AND NO TYPOS

2 Likes

The screenshot is inside of the code. Can you fix it so I can see the error?

1 Like

Ok I understand the issue, it seems that the error only happens when they leave. Soooo the script would still run and try to look for the specific playerGUI, but it doesn’t exist since the player left and that element is no longer existent. My solution would be to add an event code ‘playerRemove’ or the one similar to that and if the player left, to stop that script from running so no errors get executed into the command box. I hope you get an idea of what I’m saying!

1 Like

Not a valid member means that it is not there at the time the script runs, try using :WaitForChild instead.

1 Like

Is there any videos or articles on playerRemove event so I can see what your talking about?

^^

You can check that one out! There is also another similar event named CharacterRemoving, works the same as this PlayerRemoving event. You know the difference between a character and a player right?

The player is the entity or element in the player tab and the character is the physical player in the workspace.

1 Like

Question: How would I implent this? If statement?

1 Like

(I’m new to dev forum so idk how to do the fancy code text)

It’s a player event so you would do it IN A SERVER SCRIPT(This does NOT work on local scripts) I think your error came from server script service so gonna guess it’s a server script.

Local players = game.GetService(“Players”)

players.PlayerRemoving:Connect(function(player)

timer.Value == 0
—^ ends the timer
end)

^ just an example of how that works
You can add an if statement too that if that player is still there to then run that code else just skip it

end)[quote=“rc8s, post:4, topic:888103”]
try using :WaitForChild instead.
[/quote]

^ that is also another approach by changing line 44 with

Timer.WaitForChild(“TimerText”).text = “Game ended”

With that approach the code will get yielded until it finds the elements or that line will get skipped if the script didn’t find that elements with a warning.

1 Like

For block code its ```lua
like this:

local Hi = "Dogs are cute and fluffy."
print("Hello World!")
print(Hi)
2 Likes

You should check the spelling of each object you call to change its properties

1 Like

Hello I used the wait for child thing and i got this error

the rest of the code didnt run heres the updated code as of right now: ```

ClickButton = game.Workspace.Button

ExitPart = game.Workspace.EXITPART

local Active = true

local Players = game:GetService(“Players”)

local Outcomes = { “Obby1”, “Obby2”, “Obby3”, “Obby4”, “Obby5”, “Obby6”, “Obby7”, “Obby8”, “Obby9”, “Obby10”, “Obby11”, “Obby12”}

ClickButton.ClickDetector.MouseClick:Connect(function(player)
local GUI = player:FindFirstChild(“PlayerGui”)
local Timer = GUI:FindFirstChild(“TimerGUI”)

--if on
if Active == true then
	print("Button on")
	local Obby = game:GetService("ServerStorage")[Outcomes[math.random(1, #Outcomes)]]:Clone()
	Obby.Parent = workspace
	

	
	Active = false

-------------timer--------------------------------------------------------------------------------

	local timer = game.ReplicatedStorage.Timer

	local lengthTime = 51
	
	repeat
	for i = lengthTime, 0, -1 do
		timer.Value = i
		wait(1)
	end
	until timer.Value == 0
	
	Obby:Destroy()
	Timer:WaitForChild("TimerText").Text = "Game ended"
	Timer.TimerText.Text = "Game ended"
	ExitPart.CanCollide = false
	wait(3)
	Timer.TimerText.Text = ""
	ExitPart.CanCollide = true
	Active = true

end

–If button is off
if Active == false then
print(“Button off”)
end
end)

Ok the WaitForChild event can also include a timeOut parameter which is the longest time the script will wait if the element is present. If the element is never present in the time it was given, then it will return nil.

Can be then replaced like so:

local timerText = Timer:WaitForChild(“TimerText”,3)

If timerText ~= nil then 
timerText.Text = “Game ended”
end

Also the yellow text in the console is a warning and the red text is an error. Just thought you would know.

(Btw how do I get out of lua text?)

Sorry for late response, I was in an area with no service for a while.
1 Like