I tried to turn @x_o ‘s bmx into vehicle that you can get on and off of, but the bmx’s position does not replicate onto the server. The bmx is controlled by a local script. How can I fix this?
I tried to set all the parts’ network owner to the player but that didn’t work. It only updated when I clicked server in studio and would just explode. I don’t know what to do now.
i don’t know if reading the script will help
Control Script (local)
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
-- constants
local PLAYER = Players.LocalPlayer
local CAMERA = Workspace.CurrentCamera
local EVENTS = ReplicatedStorage:WaitForChild("Events")
local IGNORE = Workspace:WaitForChild("Ignore")
local CHARACTERS = Workspace:WaitForChild("Characters")
local CHARACTER = nil
local ACCELERATION = .8
local FLOOR_CHECK = 10
local HEIGHT = 3.5
local MODULES = ReplicatedStorage:WaitForChild("Modules")
local BIKE = require(MODULES:WaitForChild("Bike"))
local anim = script.Animation
local SPEED = 90
local JUMP = 30
-- variables
local floor = Vector3.new(0, 1, 0)
local targetVelocity = Vector3.new()
local grounded = false
-- initiate
game.ReplicatedStorage.Events.bike.OnClientEvent:Connect(function(bike, character)
print("fired client")
if character.Name == PLAYER.Name then
print("ok")
BIKE:Attach(character,bike)
CHARACTER = character
local animationTrack = PLAYER.Character.Humanoid:LoadAnimation(anim)
animationTrack:Play()
end
end)
RunService:BindToRenderStep("Control", Enum.RenderPriority.Character.Value, function(deltaTime)
if CHARACTER then
local FORCE = CHARACTER.VectorForce
local MASS = CHARACTER:GetMass()
local floorRay = Ray.new(CHARACTER.Position, -floor.Unit * FLOOR_CHECK)
local hit, position, normal = Workspace:FindPartOnRayWithIgnoreList(floorRay, {CHARACTER, IGNORE,PLAYER.Character})
local floorDistance = (position - CHARACTER.Position).Magnitude
if floorDistance <= HEIGHT then
grounded = true
else
grounded = false
end
if hit then
if grounded then
floor = normal
else
floor = floor:Lerp(normal, math.min(deltaTime * 5, 1))
end
else
floor = floor:Lerp(Vector3.new(0, 1, 0), math.min(deltaTime, 1))
end
-- floor cframe
local lookVector = CAMERA.CFrame.lookVector
lookVector = Vector3.new(lookVector.X, 0, lookVector.Z).Unit
local floorCFrame = CFrame.new(Vector3.new(), lookVector)
local localFloor = floorCFrame:vectorToObjectSpace(floor)
local x, y = math.atan2(-localFloor.X, localFloor.Y), math.atan2(localFloor.Z, localFloor.Y)
local cfA = CFrame.Angles(y, 0, 0) * CFrame.Angles(0, 0, x)
local cfB = CFrame.Angles(0, 0, x) * CFrame.Angles(y, 0, 0)
floorCFrame = floorCFrame * cfA:Lerp(cfB, 0.5)
-- input
if not UserInputService:GetFocusedTextBox() then
local input = Vector3.new()
if UserInputService:IsKeyDown(Enum.KeyCode.W) then
input = input + Vector3.new(0, 0, -1)
end
if UserInputService:IsKeyDown(Enum.KeyCode.S) then
input = input + Vector3.new(0, 0, 1)
end
if UserInputService:IsKeyDown(Enum.KeyCode.A) then
input = input + Vector3.new(-1, 0, 0)
end
if UserInputService:IsKeyDown(Enum.KeyCode.D) then
input = input + Vector3.new(1, 0, 0)
end
if input.Magnitude > 0 then
input = input.Unit
end
targetVelocity = floorCFrame:vectorToWorldSpace(input * SPEED)
end
-- apply force
if grounded then
FORCE.Force = (targetVelocity - CHARACTER.Velocity) * MASS * ACCELERATION
else
FORCE.Force = Vector3.new()
end
end
-- grounded check
end)
-- events
UserInputService.InputBegan:connect(function(inputObject, processed)
if not processed then
if inputObject.KeyCode == Enum.KeyCode.Space then
if grounded then
CHARACTER.Velocity = CHARACTER.Velocity + floor * JUMP
end
--[[elseif inputObject.KeyCode == Enum.KeyCode.E then
EVENTS.Trick:Fire(CHARACTER, "Test")
elseif inputObject.KeyCode == Enum.KeyCode.Q then
EVENTS.Trick:Fire(CHARACTER, "Test2")]]
end
end
end)
while wait(.1) do
if CHARACTER then
if CHARACTER.Parent.seat.Occupant.Parent.Name == PLAYER.Name then
end
end
end
Seat Script (server)
script.Parent.ChildAdded:connect(function(child)
if child.Name=="SeatWeld" and child:IsA("Weld") and game.Players:GetPlayerFromCharacter(child.Part1.Parent)~=nil then
--Distribute Client Interface
local p=game.Players:GetPlayerFromCharacter(child.Part1.Parent)
print(p)
print(script.Parent.Occupant.Parent)
--if p.Name == script.Parent.Occupant.Parent.Name and script.Occupant.Health > 0 then
print("ok")
local character = ServerStorage.Objects.Character:Clone()
script.Parent.Parent.PrimaryPart = character
character.Center.WorldPosition = script.Parent.Parent.Frame.Position
character.Center.Orientation = script.Parent.Parent.Frame.Orientation
character.CFrame = script.Parent.Parent.Frame.CFrame
character.Name = p.Name
character.Parent = script.Parent.Parent
for _, v in pairs(script.Parent.Parent.Seat:GetChildren()) do
if v:IsA("Motor6D") then
v:Destroy()
end
end
lastplayer = p.Name
character:SetNetworkOwner(p)
for _, v in pairs(script.Parent.Parent:GetChildren()) do
if v:IsA("BasePart") or v:IsA("VehicleSeat") then
v:SetNetworkOwner(p)
end
end
game.ReplicatedStorage.Events.bike:FireClient(p,script.Parent.Parent,character)
for _, v in pairs(p.Character:GetChildren()) do
if v:IsA("BasePart") then
PhysicsService:SetPartCollisionGroup(v,playerCollisionGroupName)
end
end
--end
end
end)
script.Parent.ChildRemoved:connect(function(child)
if child.Name=="SeatWeld" and child:IsA("Weld") then
for _, v in pairs(script.Parent.Parent:GetChildren()) do
if v:IsA("BasePart") and v.Name == lastplayer then
v:Destroy()
end
end
end
end)