I need help with this script

What I am trying to do
I am trying to make it where if 1 person click a button then it should change all the buttons to not be visible and start a countdown.

*My Issue
Whenever I click on either of the buttons on both player screens I get an error of “Players.Player2(Or Player1).PlayerGui.Start.LocalScript:59: attempt to index local ‘button’ (a nil value),” here is a screenshot of what it outputs

image

What I have tried to do so far
I have searched the DevForum, Google, Scripting help websites. I haven’t found anything specific to what I need help with and my Uncle who tells me to try to fix it for at least 30 Minutes before asking for help, I’ve been trying to fix it myself for the past 4 hours just about.

1 Like

It looks like, on line 59, you’re referencing “button”, which the script is saying doesn’t exist. Can you post the relevant sections of your code so we can take a look at why it might be erroring?

3 Likes

I am trying to use a Remote Function for the button click that way I can send it to multiple players

image

1 Like

Here is what I have Button set to, it is set as “local button” and “local counter” outside of the for loop above.

image

1 Like

Could you please provide the entire script? It’s difficult to help you since you haven’t provided the lines.
It could be that you haven’t defined button before you’re trying to access it.

Thanks!

2 Likes

I sent a screenshot to the other guy of how I defined it. I’ll send the whole script ASAP

1 Like

Are you trying to set a variable multiple times? (Hard to explain - I mean that counter and button might be overwritten for each new index of the loop)

1 Like

That may be it but it wouldn’t pop up with an error of it not having a value. Cause it’ll definitely have a value then lol

1 Like

Could try printing out the value of button every time it’s changed to isolate where the value is set to something non-existent/nil.

It’s only changed once inside of the script that I can see and that’s the spot throwing an error

@EnDarke I think I have figured out your problem you are using a “WaitForChild” function. It might be that your button you are looking for is named differently.

You snippet shows it being changed within a loop based on each player. I.e.

Players = {“EnDarke”,“Player1”,“Player2”, “Player3”}

After the first iteration, button would be equal to EnDarke.PlayerGui.Start.Button, the second time, Player1.PlayerGui.Start.Button, etc, ending on Player3.PlayerGui.Start.Button.

There’s always the possibility that there’s something wrong with the table you’re looping through and there’s 0 items in it, causing the contents of the loop to not run and the value of button to not be changed. This is why you debug rather than saying what you can see.

Try printing the value of plrs, keeping in mind that doing something along the lines of local plrs = game.Players:GetPlayers() won’t update on it’s own accord.

2 Likes

No if I take that off it still outputs and error, whenever I have fixed it all it then gives me an Infinite and doesn’t run the rest.

1 Like

I couldn’t fully understand your question because it lacks the actual code, so i hope this is what you’re looking for.


  • A LocalScript inside the button in StarterGui.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage:WaitForChild("Event")
local Button = script.Parent.Parent.TextButton

Button.MouseButton1Click:Connect(function()
	Event:FireServer()
	print("Fired server")
end)

  • A Script in ServerScriptService.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage:WaitForChild("Event")
local Plrs = game:GetService("Players")

Event.OnServerEvent:Connect(function()
	local GetPlrs = Plrs:GetChildren()
	for i = 1, #GetPlrs do
		local UI = GetPlrs[i]:WaitForChild("PlayerGui").ScreenGui.TextButton
		UI.Visible = false
	end
end)


I really hope this helps you and answers your question! :wink:

1 Like

Here is the Code. I know what is happening, I am not changing the value of said “counter” and “button” I am trying to set/change them but I just don’t know how I could change the values when it’s through a for loop of sorts without running either multiple times.

local Players = game:GetService("Players")
local plrs = {}
table.insert(Players:GetPlayers(), plrs)
local door = workspace.StartingArea:WaitForChild("Door")
local remote = game.ReplicatedStorage.Remotes.Start

function Start()
	
	local counter
	local button
	
	for i = 1, #plrs do
		
		local UI = plrs[i]:WaitForChild("PlayerGui")
		
		counter = UI.Start.Counter
		button = UI.Start.Button
		
	end
	
	for i = 5, 0, -1 do
		
		print(i)
		
		counter.Text = i
		
		if i == 0 then
			
			counter.Text = "GO!"
			
			counter:TweenSize(UDim2.new(1, 0, 1, 0), "Out", "Quart", .2)
			
			wait(.5)
			
			counter:TweenSize(UDim2.new(0, 0, 0, 0), "Out", "Quart", .2)
			
			wait(.5)
			
		else
			
			counter:TweenSize(UDim2.new(1, 0, 1, 0), "Out", "Quart", .2)
			
			wait(.2)
			
			counter:TweenSize(UDim2.new(0, 0, 0, 0), "Out", "Quart", .2)
			
			wait(.8)
			
		end
		
	end
	
	door:Destroy()
	
end

remote:InvokeServer(script.Parent.Button.MouseButton1Click:Connect(Start))```
2 Likes

I actually understood that you want a button to change for all players when a single player clicks it.
Now you are saying that you don’t know how to change the values when running a for loop without running multiple times?
Do you mean the same value or player passing through the loop again or do you mean running a complete for loop once because it runs multiple times in your code?


  • Here are some comments on your code
local Players = game:GetService("Players") 
local plrs = {} --Why do you want to make another player array when you already get one by :GetPlayers()?
table.insert(Players:GetPlayers(), plrs) --Table insert doesn't work like that, table.insert ( array t, number pos = #t+1, Variant value )
local door = workspace.StartingArea:WaitForChild("Door")
local remote = game.ReplicatedStorage.Remotes.Start

function Start()
	
	local counter
	local button
	
	for i = 1, #plrs do --You can't use a for-loop because your array is nill (wrong usage of table.insert) also using :GetPlayers()  returns 0 in for-loops
		
		local UI = plrs[i]:WaitForChild("PlayerGui") -- 0
		
		counter = UI.Start.Counter
		button = UI.Start.Button
		
	end
	
	for i = 5, 0, -1 do
		
		print(i)
		
		counter.Text = i
		
		if i == 0 then --You can just replace this statement at the end of the for loop because the for loop will stop at 0.
			
			counter.Text = "GO!"
			
			counter:TweenSize(UDim2.new(1, 0, 1, 0), "Out", "Quart", .2)
			
			wait(.5)
			
			counter:TweenSize(UDim2.new(0, 0, 0, 0), "Out", "Quart", .2)
			
			wait(.5)
			
		else
			
			counter:TweenSize(UDim2.new(1, 0, 1, 0), "Out", "Quart", .2)
			
			wait(.2)
			
			counter:TweenSize(UDim2.new(0, 0, 0, 0), "Out", "Quart", .2)
			
			wait(.8)
			
		end
		
	end
	
	door:Destroy()
	
end

remote:InvokeServer(script.Parent.Button.MouseButton1Click:Connect(Start)) -- Not the correct way to InvokeServer!

Please make sure to check these out


  • Here is a working edit of your code
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage:WaitForChild("Event")

local function Start()
	local Players = game:GetService("Players")
	local GetPlrs = Players:GetChildren()
	
	for i, Plr in ipairs(GetPlrs) do --]] Loops players
		print(Plr.Name)
		
		local PlrUI = GetPlrs[i]:WaitForChild("PlayerGui")
--		local StartUI = PlrUI.UI.Start.Counter
--		local ButtonUI = PlrUI.UI.Start.Button
--      Rest of code..
	end
	
	for i = 5, 0, -1 do --]] Countdown
--		Counter.Text = i
--      Counter:TweenSize(UDim2.new(1, 0, 1, 0), "Out", "Quart", .2)
--		wait(.2)
--		Counter:TweenSize(UDim2.new(0, 0, 0, 0), "Out", "Quart", .2)
--		wait(.8)
        print("Counter: "..i)
    end
--    	Counter.Text = "GO!"	
--	    counter:TweenSize(UDim2.new(1, 0, 1, 0), "Out", "Quart", .2)	
--		wait(.5)	
--		Counter:TweenSize(UDim2.new(0, 0, 0, 0), "Out", "Quart", .2)	
--		wait(.5)
        print("Counter: GO!")
    wait(.1)
--	door:Destroy()
    print("Removed Door")
end

-- Assuming the RemoteFunction is Client-Server
Event.OnServerInvoke = Start

By the way did the previous reply answer your question?
Also if this doesn’t answer your question may be consider messaging me.

2 Likes

I’ll check it out ASAP but thank you for your help!

2 Likes

Thank you, I will check that out asap