My projectile doesn’t sync to all clients. When i shoot the projectile, it perfectly works on my local but when on the other client’s view, the projectile moves to its direction relative to their local mouse position.
here is my script that i put on a starter player as a replicator.
local Game = game --Local environment variables.
local Workspace = workspace
local ReplicatedStorage = Game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:FindFirstChild("Remotes").SW_Rocket.SW_RocketFire or ReplicatedStorage:WaitForChild("Remotes").SW_Rocket.SW_RocketFire --RemoteEvent reference.
local fire = ReplicatedStorage:FindFirstChild("Remotes"):FindFirstChild("SW_Rocket").SW_Fire
local rocket = game:GetService("ReplicatedStorage"):FindFirstChild("Projectiles").SW_Projectiles.Fireballz
local blast = game:GetService("ReplicatedStorage"):FindFirstChild("Projectiles").SW_Projectiles.BlastExplosion
local db = false
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local function OnRemoteFired(Handle,barrel) --RemoteEvent's 'fired' function.
local newRocket = rocket:Clone()
newRocket.BrickColor = BrickColor.new("Lime green")
newRocket.CanCollide = false
newRocket.CFrame = CFrame.new(barrel.Position,mouse.Hit.Position)
newRocket.Parent = Workspace.ignore
local newForce = Instance.new("BodyVelocity")
newForce.Parent = newRocket
newForce.Velocity = (mouse.Hit.Position - barrel.Position).Unit*130
newForce.MaxForce = Vector3.new("inf","inf","inf" )
end
RemoteEvent.OnClientEvent:Connect(OnRemoteFired)
in the second argument, you are using the local player’s mouse, which is why “the projectile moves to its direction relative to their local mouse position.”
in other words, for the player that is going to shoot, you need to also send over the mouse data to the server, then from the server, send it to the clients, and use that specific mouse data to implement the projectile.
i tried your code but it everytime i hit the target it add 100 coins not only the shooter’s leaderstats but to all clients which is not what i had expected.
this is the code from serverscript service that give 100 gold to the player
fire.OnServerEvent:Connect(function(player,partHit)
local damages = {["Torso"]= 40 ,["Head"] = 200 , ["Intersection"]= 35, ["Belly"]= 25, ["LShoulder"]= 19, ["RShoulder"]= 23, ["LeftFoot"]= 20, ["LeftHand"]= 20, ["LeftLowerArm"]= 21,["LeftLowerleg"]= 22,["LeftUpperArm"]= 21, ["LeftUpperLeg"]= 22,["RightFoot"]= 20,["RightHand"]= 20,["RightLowerArm"]= 21,["RightLowerLeg"]= 22,["RightUpperArm"]= 21,["RightUpperLeg"]= 22,["Union"]= 25, ["UpperTorso"] = 45}
local Damage = 0
for _, v in pairs(partHit) do
if v.Parent:FindFirstChild("Monster") and v.Parent:FindFirstChild("Monster").Health > 0 and damages[v.Name] then
local totalDamage = Damage + damages[v.Name]
v.Parent:FindFirstChild("Monster").Health -= totalDamage
player:FindFirstChild("leaderstats"):FindFirstChild("Gold").Value = player:FindFirstChild("leaderstats"):FindFirstChild("Gold").Value + totalDamage
for i, value in pairs(v.Parent:FindFirstChild("Monster"):GetChildren()) do
if value:IsA("ObjectValue") and value.Name == "creator" then
value:Destroy()
end
local creator_Tag = Instance.new("ObjectValue")
creator_Tag.Name = "creator"
creator_Tag.Value = player
debris:AddItem(creator_Tag,2)
creator_Tag.Parent = value.Parent
end
--print(totalDamage)
elseif v.Name == "Weakpoint" and v.Parent:FindFirstChild("Monster") and not v.Parent:FindFirstChild("Boss") and v.Parent:FindFirstChild("Monster").Health > 0 then
local MonsterHealth = v.Parent:FindFirstChild("Monster").Health
v.Parent:FindFirstChild("Monster").Health -= BusterMaxDamage
player:FindFirstChild("leaderstats"):FindFirstChild("Gold").Value = player:FindFirstChild("leaderstats"):FindFirstChild("Gold").Value + BusterMaxDamage + 200
for i, value in pairs(v.Parent:FindFirstChild("Monster"):GetChildren()) do
if value:IsA("ObjectValue") and value.Name == "creator" then
value:Destroy()
end
local creator_Tag = Instance.new("ObjectValue")
creator_Tag.Name = "creator"
creator_Tag.Value = player
debris:AddItem(creator_Tag,2)
creator_Tag.Parent = value.Parent
end
if MonsterHealth <= BusterMaxDamage then
for i, value in pairs(v.Parent:FindFirstChild("Monster"):GetChildren()) do
if value:IsA("ObjectValue") and value.Name == "creator" then
value:Destroy()
end
local creator_Tag = Instance.new("ObjectValue")
creator_Tag.Name = "creator"
creator_Tag.Value = player
debris:AddItem(creator_Tag,2)
creator_Tag.Parent = value.Parent
end
--player:FindFirstChild("leaderstats"):FindFirstChild("Titan kill").Value = player:FindFirstChild("leaderstats"):FindFirstChild("Titan kill").Value + 1
player:FindFirstChild("leaderstats"):FindFirstChild("Gold").Value = player:FindFirstChild("leaderstats"):FindFirstChild("Gold").Value + MonsterHealth - BusterMaxDamage
player:FindFirstChild("PlayerGui"):FindFirstChild("ScreenGui").Frame.Visible = true
player:FindFirstChild("PlayerGui"):FindFirstChild("ScreenGui").Frame.KillSFX:Play()
print("play")
delay(1, function()
player:FindFirstChild("PlayerGui"):FindFirstChild("ScreenGui").Frame.Visible = false
end)
end
end
end
end)