This is my first question, so please go easy on me.
So I've been working on a car spawn UI, and I need to communicate from the LocalScript to the Script in the workspace which spawns the car. However, it doesn't seem to want to communicate to the RemoteFunction.
EDIT
I added a print("Invoked.")
to make sure the MouseButton1Up
event is working, and sure enough, it was. However, anything after the invoke
command is ignored.
EDIT 2
Now I'm getting errors since I followed a suggestion from the comments.
LocalScript error:
09:56:29.067 - Workspace.Part.Script:7: bad argument #2 to '?' (string expected, got Object) 09:56:29.068 - Stack Begin 09:56:29.068 - Script 'Workspace.Part.Script', Line 7 09:56:29.069 - Stack End
Script error:
09:56:29.102 - Workspace.Part.Script:7: bad argument #2 to '?' (string expected, got Object) 09:56:29.103 - Stack Begin 09:56:29.103 - Script 'Players.Pixelated_MC.PlayerGui.SpawnCarUI.Frame.LocalScript', Line 14 09:56:29.103 - Stack End
LocalScript:
script.Parent.ChooseCar.ChooseCar.MouseButton1Up:Connect(function() script.Parent.ChooseCar.Visible = false script.Parent.CarList.Visible = true script.Parent.Title.Text = "Please choose a car from the list." end) script.Parent.ChooseCar.CustomCar.MouseButton1Up:Connect(function() script.Parent.ChooseCar.Visible = false script.Parent.CustomCar.Visible = true script.Parent.Title.Text = "Please enter an ID below." end) for i,v in pairs(script.Parent:WaitForChild("CarList"):GetChildren()) do if v.Name == "Item" then v.Icon.Spawn.MouseButton1Up:Connect(function() script.Parent.Original.Value:Invoke(v.Spawn.Text) print(script.Parent.Original.Value) end) end end
Script:
script.Parent.ClickDetector.MouseClick:Connect(function(player) script.Parent.BrickColor = BrickColor.Red() local gui = script.Parent.SpawnCarUI:Clone() gui.Frame.Original.Value = script.Parent.SpawnCarFunction gui.Parent = player.PlayerGui script.Parent.SpawnCarFunction.OnInvoke = function(carName) local car = game.ServerStorage.Cars[carName]:Clone() car.Parent = game.Workspace car:MoveTo(game.Workspace.SpawnLocation.Position) end end)
You can view the image of my explorer here.
I have no errors, so what is the problem?
Thank you.
I have found the issue after messing with my code for a long time.
Here is what I did:
I needed to replace the parameters in the receive code with player, carName
for it to work.
Server Script under "Part"
local remote = Instance.new("RemoteFunction") remote.Name = "SpawnCarFunction" remote.Parent = game:GetService("ReplicatedStorage") script.Parent.ClickDetector.MouseClick:Connect(function(player) if script.Parent.BrickColor ~= BrickColor.new("Really red") then script.Parent.BrickColor = BrickColor.new("Really red") local response = remote:InvokeClient(player) print(response) end end) function remote.OnServerInvoke(player, carName) local car = game:GetService("ServerStorage"):WaitForChild("Cars")[carName]:Clone() car.Parent = workspace car:MoveTo(workspace.SpawnLocation.Position) return("Complete") end
Local Script under StarterGui
local remote = game:GetService("ReplicatedStorage"):WaitForChild("SpawnCarFunction") function remote.OnClientInvoke() script.Parent.Parent.Enabled = true return("Connected") end script.Parent:WaitForChild("ChooseCar"):WaitForChild("ChooseCar").MouseButton1Up:Connect(function() script.Parent.ChooseCar.Visible = false script.Parent.CarList.Visible = true script.Parent.Title.Text = "Please choose a car from the list." end) script.Parent:WaitForChild("ChooseCar"):WaitForChild("CustomCar").MouseButton1Up:Connect(function() script.Parent.ChooseCar.Visible = false script.Parent.CustomCar.Visible = true script.Parent.Title.Text = "Please enter an ID below." end) for i, v in pairs(script.Parent:WaitForChild("CarList"):GetChildren()) do if v.Name == "Item" then v:WaitForChild("Icon"):WaitForChild("Spawn").MouseButton1Up:Connect(function() script.Parent.Parent.Enabled = false local response = remote:InvokeServer(v:WaitForChild("Spawn").Text) print(response) end) end end
You can remove "Original" and the BindableEvent "SpawnCarFunction"
I used a check on the "Part"'s BrickColor to determine if the event can occur (if its red, no other player can use it)
I also made sure the "SpawnCarUI" had enabled set to false while it was in StarterGui