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

What is wrong with this "spawn car gui"? nothing happens when i click in the textbutton

Asked by 4 years ago
script.Parent.MouseButton1Click:connect(function() 
local player = game.Players.LocalPlayer 


local car = game.Lighting.CarOne:Clone()

if car ~= nil then

car:MoveTo(player.Character.Head.Position) end end)


0
Try setting the parent of the clone > car.Parent = workspace Arkrei 389 — 4y

1 answer

Log in to vote
0
Answered by
Lazarix9 245 Moderation Voter
4 years ago

Since this is a LocalScript, the car will be spawned on the players head, and they only will be able to see it, so you will need two scripts. Having that in mind you will need to have a Remote Event to communicate between the scripts(one server script and a LocalScript), so you go to ReplicatedStorage and add a RemoteEvent in there; you could also name it CarSpawnEvent or something. Now that we have a RemoteEvent, you can add a LocalScript to the button:

--LocalScript

--Variables
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local CarSpawnEvent = ReplicatedStorage:WaitForChild('CarSpawnEvent')
--Code
script.Parent.MouseButton1Click:Connect(function()

    CarSpawnEvent:FireServer() --Used to send a message to the server from a client

end)

Now put a server script in Workspace, or ServerScriptStorage and write this in it:

--Script:

--Variables
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local CarSpawnEvent = ReplicatedStorage:WaitForChild('CarSpawnEvent')
local Lighting = game.Lighting
local Car = Lighting:WaitForChild('CarOne')
--Functions
local function onCarSpawn(plr)
    local cclone = Car:Clone()

    cclone.Parent = game.Workspace --To set the parent to workspace
    cclone.Position = plr.Character.Head.Position + Vector3.new(3,3,3) --So the car doesnt spawn ON the players head.

end 

--Code
CarSpawnEvent.OnServerEvent:Connect(onCarSpawn) --To connect to the function if the RemoteEvent has been fired by the client.

You could also name the script something like 'CarSpawn' so you dont mix it up with other scripts.

0
annoys me that a lot of u use local function() instead of just doing as in this case CarSpawnEvent.OnServerEvent:Connect(function(plr) this would do the exact same thing but saves a bit of scripting. but this still works Gameplayer365247v2 1055 — 4y
0
I like to do it this way. It is easy to change if you prefer it like what you just said though :P Lazarix9 245 — 4y
Ad

Answer this question