Button activation with loop

Heyo,
I’ve recently ran into some trouble trying to use multiple buttons with a for loop, where it keeps executing the function without me pressing any button and something about a nil value while there should be a value. It’s probably something really stupid, but I can’t seem to find what’s causing the issue.
So what I’m trying to do is spawn a ship model server side in a “Ship” folder in the Workspace after pressing a button that’s generated from a set of models in ReplicatedStorage.

Local script:

local gui = script.Parent
local frame = gui.Frame
local scrollFrame = frame.ScrollingFrame 

local repStorage = game.ReplicatedStorage
wait(2)
local ships = repStorage.Ships:GetChildren()

--Calls server script to spawn a ship
local shipSpawn = function(ship)
	print("test2")
	repStorage.ShipSpawn.RemoteEvent:FireServer(ship)
end

--Makes buttons for each type of ship
for i = 1, #ships, 1 do
	local btn = Instance.new("TextButton", scrollFrame)
	btn.Size = UDim2.new(1,0,0.1,0)
	btn.Position = UDim2.new(0, 0, (i-1)*.1,0)
	btn.Text = ships[i].Name
end

--Executes the shipSpawn function for each button
for i, button in ipairs(scrollFrame:GetChildren()) do
	if button:IsA("TextButton") then
		print(button.Text)
		button.MouseButton1Click:Connect(shipSpawn(button.Text))
	end
end

--GUI setup
--GUI > Frame > ScrollFrame > Buttons for ship spawning


^ Everything is being executed on the start of the game for some reason and the “attempt to call a nil value” is shown after pressing a button.

You declared the variable ships but you didn’t declare ship, which is the problem, I think.

Also at the function shipspawn() you wrote

local shipSpawn = function(ship)
instead of

local function shipSpawn(ship)

Should be passed down from the button activation if I’m getting this right

Seems to have the same issue, I think both methods have the same working

Could you try out button.MouseButton1Click:Connect(function() shipSpawn(button.Text) end instead of button.MouseButton1Click:Connect(shipSpawn(button.Text)). the compiler is complaining that no function is being called in the :Connect() so for some reason it doesn’t recognize shipspawn as a function.

1 Like

It doesn’t recongnize it because like I already said he declared it like this:
local shipSpawn = function(ship)

1 Like

The other ways to write functions give the same problem, and the “button.MouseButton1Click:Connect(function() shipSpawn(button.Text) end” didn’t work either

But why didn’t the other way to write functions work (Dyzody’s methode is the standard and only way i know of how to write functions, so for readability it’s probably good to use it!) and why did rewriting the connect not work? did you get different error’s?

No idea why they didn’t work, same errors, so I think all methods work the same way.
The connect rewriting gave parenthesis errors, tried to replace the end with a ) but that didn’t work either.

Maybe try to make a function to the connect before calling the shipspawn function becouse the mousebutton1click already comes with its own returns

button.MouseButton1Click:Connect(function() shipSpawn(button.Text)
end)

1 Like

Aight, thanks a lot, ScriptArchiver and Di_Rect (forgot the “)” at the end), this along with moving the script from ReplicatedStorage to Workspace fixed the problem!

Sorry about that, I sometimes forget one!

local gui = script.Parent
local frame = gui.Frame
local scrollFrame = frame.ScrollingFrame 

local repStorage = game.ReplicatedStorage
local shipFolder = repStorage:WaitForChild("Ships")
local ships = shipFolder:GetChildren()

--Calls server script to spawn a ship
local shipSpawn = function(ship)
	print("test2")
	repStorage.ShipSpawn.RemoteEvent:FireServer(ship)
end

--Makes buttons for each type of ship
for i = 1, #ships, 1 do
	local btn = Instance.new("TextButton", scrollFrame)
	btn.Size = UDim2.new(1,0,0.1,0)
	btn.Position = UDim2.new(0, 0, (i-1)*.1,0)
	btn.Text = ships[i].Name
end

--Executes the shipSpawn function for each button
for i, button in ipairs(scrollFrame:GetChildren()) do
	if button:IsA("TextButton") then
		print(button.Text)
		button.MouseButton1Click:Connect(function()
			shipSpawn(button.Text)
	end)
end

--GUI setup
--GUI > Frame > ScrollFrame > Buttons for ship spawning

Here’s the script with the fix & some slight optimisations.