Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Car spawner does not seem to work?

Asked by 5 years ago
Edited 5 years ago

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.

0
SpawnCarUI is a Screen Gui, try moving it to the Starter Gui section of the game first SerpentineKing 3885 — 5y
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. UnitedGateCompany 18 — 5y
0
The RemoteFunction has the events: InvokeServer and OnServerInvoke, not "Invoke" EDIT: Noticed this is a Bindable Event, XD SerpentineKing 3885 — 5y
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. UnitedGateCompany 18 — 5y
View all comments (2 more)
0
Is there a "spawn" under the icon? SerpentineKing 3885 — 5y
0
Yes. UnitedGateCompany 18 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

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.

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

0
Thanks for posting this! I will try. UnitedGateCompany 18 — 5y

Answer this question