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)
What is wrong with this "spawn car gui"? nothing happens when i click in the textbutton
1 answer
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.
2 comments
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
I like to do it this way. It is easy to change if you prefer it like what you just said though :P
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
Try setting the parent of the clone