Server client problem

  1. I Want to make a fireball.

  2. The fireball gets delayed in the client, So, In the server, It is not in the same position as the client.

When the player clicks the tool fire server to a server script in the tool after that, The fireball gets cloned and edited and Then applies a linear velocity to it and Gets fire.

Server script:

--Server
local Tool = script.Parent
local Remotes = Tool["Remotes."]



Remotes["Equipped."].OnServerEvent:Connect(function(Player)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	Character["Right Arm"]:FindFirstChild("RightGrip"):Destroy()
	local Joint = Instance.new("Motor6D")
	Joint.Name = "RightGrip"
	Joint.Parent = Character["Right Arm"]
	Joint.Part0 = Character["Right Arm"]
	Joint.Part1 = Tool.Handle
end)

Remotes["Unequipped."].OnServerEvent:Connect(function(Player)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	Character["Right Arm"]:FindFirstChild("RightGrip"):Destroy()
end)

Remotes["Activated."].OnServerEvent:Connect(function(Player, Direction: Vector3)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local Sound = Instance.new("Sound")
	Sound.SoundId = "rbxassetid://17299679453"
	Sound.Parent = Character.PrimaryPart
	Sound:Play()
	
	local FireBall = nil
	if Player.Team ~= game.Teams["Wizard Outlaws"] and Player.Team ~= game.Teams["Wizard Prisoners"] then
		FireBall = Tool["Gaurds fire particle"]:Clone()
	elseif Player.Team == game.Teams["Wizard Outlaws"] or Player.Team == game.Teams["Wizard Prisoners"] then
		FireBall = Tool["Outlaws fire particle"]:Clone()
	end
	FireBall.Parent = Character
	FireBall:SetAttribute("Enabled", true)
	FireBall:SetAttribute("PlayerName", Player.Name)
	for Index, Descendant in FireBall:GetDescendants() do
		if Descendant:IsA("ParticleEmitter") and Descendant.Parent.Name ~= "Fire." then
			Descendant.Enabled = true
		end
	end

	FireBall.CFrame = Character["Right Arm"].CFrame * CFrame.new(0,0,-4)
	local BodyForce = Instance.new("BodyForce")
	BodyForce.Parent = FireBall
	BodyForce.Force = Vector3.new(0,workspace.Gravity * FireBall:GetMass(),0)
	
	local LinearVelocity = Instance.new("LinearVelocity")
	LinearVelocity.Parent = FireBall
	LinearVelocity.Attachment0 = FireBall.Attachment
	LinearVelocity.VectorVelocity = CFrame.new(Character.HumanoidRootPart.Position, Direction).LookVector * 150
end)

Client script:

local Tool = script.Parent
local Remotes = Tool["Remotes."]
local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid: Humanoid = Character:WaitForChild("Humanoid")
local Animator: Animator = Humanoid:WaitForChild("Animator")

local WandIdleAnimation = Animator:LoadAnimation(Tool["Wand idle animation."])
local AttackAnimation = Animator:LoadAnimation(Tool["Wand Animation."])

local Cooldown = 0

Tool.Equipped:Connect(function()
	Remotes["Equipped."]:FireServer()
	WandIdleAnimation:Play()
end)

Tool.Unequipped:Connect(function()
	Remotes["Unequipped."]:FireServer()
	WandIdleAnimation:Stop()
end)

Tool.Activated:Connect(function()
	if Cooldown > 0 then return end
	local Mouse = Player:GetMouse()
	local MousePosition = Mouse.Hit.Position
	Tool["Remotes."]["Activated."]:FireServer(MousePosition)
	AttackAnimation:Play()
	Cooldown = 1
end)


coroutine.wrap(function()
	while wait(1) do
		Cooldown = Cooldown - 1
	end
end)()

Demonstration. (youtube.com)

1 Like

you can never eliminate delay completly. there will always be a bit of delay between the server and the clients

1 Like

I Apologies, I Forgot the video demonstration.

i checked the video and i dont see what the problem is? again, you cant completly eliminate delay.

If you feel like you are experiencing abnormal amounts of delay try checking the “IncomingReplicationLag” setting in your studio settings

It is 0, Have you seen the delay in the video?

okay so what im guessing is you anchor the fireball after it hits something?

This might cause problems since the fireball is in a different position on the client than on the server when it gets anchored. ive had a similar problem before and an easy way to fix it is to move the fireball up by 0.001 then down by 0.001. That used to work for me but you can try for yourself and see. (Move the fireball after its anchored)

1 Like

I Am thankful, But, Could you please explain more about what you mean by moving it up and Down?

Maybe you would possibly benefit from using task.wait instead.

Fireball.Position.Y += 0.001
Fireball.Position.Y -= 0.001

Where can I do that exactly, If you could please explain.

(deleted post cause i responded to the wrong thing)

I think this is due to high ping.
Ping is basically the time it takes from the client to send information to the server and get a response back.

No, I Do not agree, It happen even if there was almost no lag.

That’s moslty because you’re doing it in a wrong way, the fireball projectile and effects should be made on the client side.

The correct way to do it is:

  1. Client: Tool activated > Do some check (cooldowns ect
) > do the fireball projectile + effects > FireServer(Mouse.Position)

  2. Server: Do some check (cooldowns ect
) > FireAllClient(OriginalPlayer, TargetPosition) > cast damage

  3. All Clients: do the fireball projectile + effects only if the OriginalPlayer is not you (not OriginalPlayer == Local Player)

This way, the fireball will be instantly thrown from your point of view, the server will do some sanity checks to see if there’s no exploits and then cast the damages and replicate the fireball to all other players point of view on their client side.

Lag does not equal ping.

charcharchar

Ensure youre parenting things LAST

This way the server does not have to replicate the information you change after it gets parented to every single client (This can be bad on performance and maybe cause slight delays)

How exactly, I Have put all of the “.parent =” in the last of the script and it is still the same.

Like I said it may not have a large effect on performance, but it’s good if you’re trying to optimize this code to work faster.

And you don’t have to put the parents last last, just make sure it’s the last thing you do AFTER changing variables specifically for the thing you’re thing to parent

This way if you change say, volume and name of a sound object after it’s been parented, those properties don’t have to be replicated to EVERY client at the same time, instead parent before changing variables, so when the object does get parented all the client has to do is receive one object instead of an object and 2 more property changes afterwards.

It’s hard to explain


So, I dont know how to fix this problem, but it APPEARS like something has gone wrong with the bodyforce creation or something, on the client.

If you watch closely the fireball is not moving on the client, but it spawns in the same place as it does on the server.

I dont think its a delay issue, I cant even see any.

I think you should add some prints, check the fireball for a line force on the client. Just try stuff related to this and you might have some results.