So i have a car spawner GUI, and when ever i spawn in a vehicle, it always spawns inside me. So instead i need it to move like 5 or 10 studs in front of the player when it spawns. How would i do this? Help much appreciated!
local model = game.ServerStorage.Vehicles:FindFirstChild(vehicles[index][1]):Clone()
model.Name = vehicleName
model.Parent = game.Workspace
model:MakeJoints()
model:MoveTo(player.Character.HumanoidRootPart.Position + Vector3.new(0, 2, 0))
local cframe = model.PrimaryPart.CFrame -- Current CFrame
model:SetPrimaryPartCFrame(CFrame.new(player.Character.HumanoidRootPart.Position, player.Character.HumanoidRootPart.Position + player.Character.HumanoidRootPart.CFrame.LookVector))
10 Likes
[[ LocalScript]]
local car = game.ReplicatedStorage --Make the Parent of your Car the ReplicatedStorage, else it will not work
local player = game.Players.LocalPlayer --Make sure that this is a LocalScript
local RE = istance.new("Remote Event", ReplicatedStorage)
script.Parent.MouseButton1Click:Connect(function() --if your ButtonGui is Clicked then …
RE:FireServer(player, "Car")
end)
[[ServerScript]]
local RE = game.ReplicatedStorage:WaitForChild("RE")
RE.OnServerEvent:Connect(function(player, Key)
if Key == "Car" then
local Model = car:Clone() --Then clone the Car from ReplicatedStorage
Model:SetPrimaryPartCFrame(player.CFrame*CFrame.new(0,3,10)) --And Spawn the Car 10 Studs front of the player and 3 Studs above
local BodyPosition = istance.new("BodyPosition", Model)
BodyPosition. position = Vector3.new(0,0,10)*player.Character.HumanoidRootPart.CFrame.lookVector
BodyForce. maxForce = Vector3.new(500000000, 500000000, 500000000)
end
end)
it doesn’t appear to work. It still spawns in the same position. maybe you could try to use model:MoveTo()? or something?
EDIT: i just noticed in my script i have a second line of code: model:SetPrimaryPartCFrame(CFrame.new(player.Character.HumanoidRootPart.Position, player.Character.HumanoidRootPart.Position + player.Character.HumanoidRootPart.CFrame.LookVector))
Could you convert it so it also moves it forwards?
2 Likes
MoveTo()
is a fuction if your Model has a Humanoid
Have you try to set a PrimaryPart To your Model?
2 Likes
What do you mean??? Do that once spawned that it also moves a little bit forward?
yes, so it spawns at your characters pos, and orientation and it moves forward from where the character is facing
1 Like
here try this instead:
--try doing this instead:
--local script
local RemoteEvent = --put a reference to the remote event here
local Button = --put a reference to the button here
Button.MouseButton1Down:Connect(function()
RemoteEvent:FireServer()
end)
--Server script
local RemoteEvent = --reference to remote event here
local Car = --reference to the car model here
local DIST_FROM_PLAYER = 10--distance it will spawn in-front of player change to your liking
RemoteEvent.OnServerEvent:Connect(function(player)
local Character = player.Character or player.CharacterAdded:Wait()
local NewCar = Car:Clone()
NewCar.Parent = workspace
NewCar:SetPrimaryPartCFrame(CFrame.new(Character.PrimaryPart.CFrame.Position+(DIST_FROM_PLAYER*Character.PrimaryPart.CFrame.LookVector)))
end)
thanks, but how would i get it to rotate and face where the player is facing?
set the cframe to this:
NewCar:SetPrimaryPartCFrame(CFrame.new(Character.PrimaryPart.CFrame.Position+(DIST_FROM_PLAYER*Character.PrimaryPart.CFrame.LookVector),Character.PrimaryPart.Position))*CFrame.Angles(0, math.rad(180), 0))
--CFrame.new(Position,PositionToLookAt)
4 Likes
thanks, but the vehicle faces backwards when i spawn it in. How would i fix this?
First you want to get your client code done and create a remote event called “SpawnCar” in ReplicatedStorage.
Place your local script under the button you want to use.
script.Parent.MouseButton1Click:Connect(function()
game.ReplicatedStorage.SpawnCar:FireServer()
end)
Now when the player clicks the button it will fire the SpawnCar event.
Now we want to actually spawn the car in via a script in server script service.
game.ReplicatedStorage.SpawnCar.OnServerEvent:Connect(function(plr)
local car = game.ServerStorage.CarModelHere:Clone
car:SetPrimaryPartCFrame(plr.Character.PrimaryPart.CFrame + Vector3.new(10, 3, 0))
car.Parent = workspace
end)
The cars primary part is usually the vehicle seat, we are spawning it +3 studs in the air to avoid any collisions with the floor when spawned.
1 Like
Try adding CFrame.Angels to the code of @Romeo_Duncan.
does it still face backward if it does try this instead:
local Eye,Target = Character.PrimaryPart.CFrame.Position+(DIST_FROM_PLAYER*Character.PrimaryPart.CFrame.LookVector),Character.PrimaryPart.Position
local RightVector = (Eye-Target).Unit:Cross(Vector3.new(0,1,0))
local UpVector = RightVector:Cross((Eye-Target).Unit)
local TargetCFrame = CFrame.fromMatrix(Eye,RightVector,UpVector )
NewCar:SetPrimaryPartCFrame(TargetCFrame)
Edited, also you need to make sure the primary part of the car is actually oriented correctly and facing the front of the car, to check which fact is the front face go to surfaces under property tab and click on the front surface and it should be highlighted
its alright, Eternalove_fan32 fixed it
hey um, your code doesn’t work, theres always an error and it has unnecessary text, like local Eye,Target? the “,” for example.
not thats multiple assigment, whats the error ur getting i did it online let me look at it again
its line 4. expected identifier when parsing expression, got local.
I think you could use a “Weld” in this case. This code example shows you how to create a Board in front of your character no matter how your character moves.
-- change board to your model
local board = Board:Clone()
local character = LocalPlayer.Character
local HumanoidRootPart = character:FindFirstChild("HumanoidRootPart")
-- to put your model in front of your player
board.CFrame = HumanoidRootPart.CFrame * CFrame.new(0,15,-10)
board.Anchored = false
board.Parent = game.Workspace
local weld = Instance.new("Weld")
weld.Part0 = board
weld.C0 = board.CFrame:Inverse()
weld.Part1 = HumanoidRootPart
weld.C1 = HumanoidRootPart.CFrame:Inverse()
weld.Parent = board