hi, im trying to get a boat to spawn from a simple gui when a player clicks a button, though the remotefunction doesnt seem to work too well as the script is casting a weird attempt to index nil value error when it doesnt try to index any value at all, all its trying to do is send a invokeserver, ive tried without the "boat" part in the invokeserver event
Output: 18:14:35.653 - Workspace.BSPP1.Script:13: attempt to index a nil value 18:14:35.654 - Stack Begin 18:14:35.654 - Script 'Players.misterviggo.PlayerGui.BoatSpawnGui.LocalScript', Line 11 18:14:35.654 - Stack End
BSel = script.Parent.BoatSelector BFrame = script.Parent.BaseFrame BGUI = script.Parent ExBut = script.Parent.BaseFrame.ExitButton ExBut.MouseButton1Click:Connect(function() BGUI.Enabled = false end) BSel.MBoat1.MouseButton1Click:Connect(function() game.Workspace.BSPP1.SpawnReFunction:InvokeServer("Boat") end)
Server Script:
Spawnable = 0 Coordinates = 153.375, 71.75, 43 script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChildOfClass("Humanoid") then end end) script.Parent.SpawnReFunction.OnServerInvoke = function(Boat) if Spawnable == 0 then local BReplica = game.ReplicatedStorage:FindFirstChild(Boat):Clone() BReplica.Name = Boat BReplica.Parent = game.Workspace BReplica:SetPrimaryPartCFrame(CFrame.new(Coordinates)) end end
It's hard to tell which line your program is erroring on, since the lines you posted don't match up with what their actual lines in game are.
I'm pretty sure it's erroring because in your function that catches the remote event, you're forgetting that the player is the first parameter since it is implicitly passed. Your boat is the second parameter, not the first, but you're naming the player parameter 'Boat'. The player doesn't exist in the replicated storage so the game will error.
Spawnable = 0 --EDIT: this needs to be a cframe. i THINK lua will ignore the rest of the variables leaving --coordinates to be only 153.375 (either that or itll error) --so made sure to wrap this in a cframe.new() so it does what you want it to do Coordinates = CFrame.new(153.375, 71.75, 43) script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChildOfClass("Humanoid") then --player has touched the script.Parent end end) --EDIT: I changed how the function is ran to be :Connect(function() instead of assigning the function to the OnServerInvoke -- This is where your issue probably is. --The first parameter here should be the player that invoked it, not the boat itself since --the player is implicitly passed script.Parent.SpawnReFunction.OnServerInvoke:Connect(function(playerWhoCalledEvent, Boat) if Spawnable == 0 then local BReplica = game.ReplicatedStorage:FindFirstChild(Boat):Clone() BReplica.Name = Boat BReplica.Parent = game.Workspace BReplica:SetPrimaryPartCFrame(Coordinates) end end
In case that wasn't the issue, I added some waitForChild() functions. The script might be running before the everything else inside the object was loaded. Look at the comments that say TODO: and make sure to do what it says there in case you're still having the error.
boatGui = script.Parent boatSelector = boatGui:WaitForChild("BoatSelector") baseFrame = boatGui:WaitForChild("BaseFrame") exitButton = baseFrame:WaitForChild("ExitButton") exitButton.MouseButton1Click:Connect(function() boatGui.Enabled = false end) --TODO: Check if this path exists vvv boatSelector.MBoat1.MouseButton1Click:Connect(function() --TODO: Put your remote function inside the ReplicatedStorage --It is currently confusing since you're invoking the server with an object in the server --Check if this path exists vvv game.Workspace.BSPP1.SpawnReFunction:InvokeServer("Boat") end)
Spawnable = 0 Coordinates = 153.375, 71.75, 43 script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChildOfClass("Humanoid") then end end) script.Parent.SpawnReFunction.OnServerInvoke = function(Boat) if Spawnable == 0 then local BReplica = game.ReplicatedStorage:FindFirstChild(Boat):Clone() BReplica.Name = Boat BReplica.Parent = game.Workspace BReplica:SetPrimaryPartCFrame(CFrame.new(Coordinates)) end end