So i have been coding some crash physics stuff and wanted to replicate it from server to all clients for smoother performance and ive ran into a problem. When a player rams a prop, it gets destroyed but only the player who rammed it sees it get destroyed, not the other players.And also how can i make my code faster? Here is my code
server script:
local Model = script.Parent.Parent.Parent
local VehicleSeat = Model:FindFirstChildOfClass("VehicleSeat")
local OwnerValue = Model.OwnerValue
local Debris = game:GetService("Debris")
local CollectionService = game:GetService("CollectionService")
--Remotes
local Remotes = game:GetService("ReplicatedStorage"):WaitForChild("Remotes")
local OnCollisionEvent = Remotes:WaitForChild("OnCollision")
local PreviousDriver = nil
local CollisionParts = Model.CollisionParts
local function SetNetworkOwner(owner)
if owner then
OwnerValue.Value = owner
for i, part in ipairs(Model:GetDescendants()) do
if part:IsA("BasePart") then
local CanSet = part:CanSetNetworkOwnership()
if CanSet == true then
part:SetNetworkOwner(owner)
end
end
end
end
end
local function CheckNetworkOwner()
if VehicleSeat.Occupant then
local player = game.Players:GetPlayerFromCharacter(VehicleSeat.Occupant.Parent)
if player then
SetNetworkOwner(player)
end
else
if OwnerValue.Value == nil and PreviousDriver then
SetNetworkOwner(PreviousDriver) --Set the ownership to the previous driver
end
end
end
local function OnCollision(hit)
if hit:IsA("BasePart") and CollectionService:HasTag(hit.Parent, "Prop") then
for i, part in pairs(hit.Parent:GetDescendants()) do
if part:IsA("BasePart") then
OnCollisionEvent:FireAllClients(VehicleSeat, part, hit)
end
end
end
end
VehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
CheckNetworkOwner()
end)
game.Players.PlayerAdded:Connect(function(player)
PreviousDriver = player
CheckNetworkOwner()
end)
for i, part in pairs(CollisionParts:GetDescendants()) do
if part:IsA("BasePart") then
part.Touched:Connect(OnCollision)
end
end
local script:
local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character
local Humanoid = Character:FindFirstChildOfClass("Humanoid") or Character:WaitForChild("Humanoid")
--Remotes
local Remotes = game:GetService("ReplicatedStorage"):WaitForChild("Remotes")
local OnCollisionEvent = Remotes:WaitForChild("OnCollision")
local function PropCollisionEvent(VehicleSeat, part)
VehicleSeat.PropCollision:Play()
part:BreakJoints()
part.Anchored = false
part.CanCollide = true
part.CanTouch = false
part.Massless = true
local forceMultiplier = math.floor(VehicleSeat.AssemblyLinearVelocity.Magnitude)
local direction = (VehicleSeat.CFrame.LookVector).unit
local force = direction * forceMultiplier
part.AssemblyLinearVelocity = force
end
OnCollisionEvent.OnClientEvent:Connect(PropCollisionEvent)