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
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.
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.
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!
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.