Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Car spawner does not seem to work?

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.

6 comments

0

SpawnCarUI is a Screen Gui, try moving it to the Starter Gui section of the game first

0

Yeah I know, but I want it in the button so only one person at a time can use the UI, and I'm going to make a script which closes the garage door to prevent anyone from tampering the other player's spawn car.

0

The RemoteFunction has the events: InvokeServer and OnServerInvoke, not "Invoke" EDIT: Noticed this is a Bindable Event, XD

0

Did you look at the image of my explorer? You might be able to better understand it. I will try to do what you are saying.

0

Is there a "spawn" under the icon?

0

Yes.

2 answers

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

1 comment

0

Thanks for posting this! I will try.

This discussion is preserved as it was. New answers, comments, and votes are closed.