So i got a script in ServerScriptService which loops trough the CollectionService, to check for any models with the Tag “Car”. If there is, there is a whole script which will make the car work on so on.
However, i wanted to be able to flip the car, when its fallen over. I added this code:
local function resetCarOrientation()
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.KeyCode == Enum.KeyCode.F and not gameProcessed then
if car.PrimaryPart then
local position = car.PrimaryPart.Position
local currentOrientation = car.PrimaryPart.Orientation
local resetCFrame = CFrame.new(position) * CFrame.Angles(math.rad(currentOrientation.X), math.rad(currentOrientation.Y), 0)
car:SetPrimaryPartCFrame(resetCFrame)
else
warn("Something wrent horribly wrong!")
end
end
end)
end
However, i tried printing to see if anything happens when pressing F. Nothing happened.
My idea is, its because of the loop. However i cant get the car model an other way (I know) to make this car flip script work.
No, however you can use a remote event to communicate between client and server.
local script
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.KeyCode == Enum.KeyCode.F and not gameProcessed then
RemoteEvent:FireServer(TheCarYouAreDriving)
end
end)
Server Script
RemoteEvent.OnServerEvent:Connect(function(Player, car)
if car.PrimaryPart then
local position = car.PrimaryPart.Position
local currentOrientation = car.PrimaryPart.Orientation
local resetCFrame = CFrame.new(position) * CFrame.Angles(math.rad(currentOrientation.X), math.rad(currentOrientation.Y), 0)
car:SetPrimaryPartCFrame(resetCFrame)
else
warn("Something wrent horribly wrong!")
end
end
A local script in a gui (Speedometer speed displayer)
UserInputService.InputBegan:Connect(function(key, gameProcessed)
if key == Enum.KeyCode.F and not gameProcessed and not debounce then
debounce = true
EventsFolder.CarEvents.FlipCar:FireServer(car)
task.wait(.5)
debounce = false
end
end)
The main car script:
EventsFolder.CarEvents.FlipCar.OnServerEvent:Connect(function(carToFlip)
if carToFlip then
if carToFlip.PrimaryPart then
local position = carToFlip.PrimaryPart.Position
local currentOrientation = carToFlip.PrimaryPart.Orientation
local resetCFrame = CFrame.new(position) * CFrame.Angles(math.rad(currentOrientation.X), math.rad(currentOrientation.Y), 0)
carToFlip:SetPrimaryPartCFrame(resetCFrame)
else
warn("Something wrent horribly wrong!")
end
end
end)