Projectile not going in the correct direction and stays stuck at it's origin

So when the client tries to fire a Projectile it sends a signal to the server, the server processes it and fires a remote to all clients, im Currently using SimpleCast, it does replicate the projectile aross all clients, but from other clients perspective they see the projectile stuck at it’s origin: Image (1 is the client who threw the projectile, it looks correct, but for other clients (2) it doesn’t look right, it’s stuck at the origin.)

Replicating:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage.Events

local Replication = {}

function Replication.ValidateProjectile(Player, MousePosition: number, MaxDistance: number)
	local Root = Player.Character.PrimaryPart
	
	local CastParams = RaycastParams.new()
	CastParams.FilterType = Enum.RaycastFilterType.Exclude
	CastParams.FilterDescendantsInstances = { Player.Character }
	
	local Origin = Root.Position
	local Direction = (MousePosition - Origin).Unit * MaxDistance
	
	Events.ReplicateProjectile:FireAllClients(Origin, Direction)
	
	local RaycastResult = workspace:Raycast(Origin, Direction, CastParams)
	if RaycastResult then
		print(RaycastResult.Instance.Name)
	end
end

function Replication.Start()
	Events.ReplicateProjectile.OnServerEvent:Connect(Replication.ValidateProjectile)
end

return Replication

Visualizing projectile across all clients

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage.Events

local SimpleCast = require(ReplicatedStorage.Modules.Packages.SimpleCast)

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local CastParams = RaycastParams.new()
CastParams.FilterType = Enum.RaycastFilterType.Exclude
CastParams.FilterDescendantsInstances = { Character }

local Projectile = ReplicatedStorage.Assets.Projectile:Clone()

local Caster = SimpleCast.new(Projectile, {
	MaxLifeTime = 5,
	CacheSize = 25,
	CacheGrowthSize = 10,
	Gravity = Vector3.new(0, -50, 0),
	RaycastParam = CastParams,

	OnCastHit = function()

	end,

	OnCastMove = nil,
	OnCastTerminate = nil
})

local HandleProjectile = {}

function HandleProjectile.Start()
	Events.ReplicateProjectile.OnClientEvent:Connect(function(Origin: Vector3, Direction: Vector3)
		Caster:FireCast(Direction, Origin)
	end)
end

return HandleProjectile

NOTE: I’ve aleardy tried to debug it, it sends the correct data and it receives the correct data, issue might lie in Caster:FireCast(Direction, Origin), this is how SimpleCast fires their projectiles:

function SimpleCast:FireCast(initVelocity: Vector3, initPosition: Vector3): CastData
	debug.profilebegin("SimpleCastFire")
	
	local newCast: CastData = self._casts:GetFromCache()
	newCast.Object.Position = initPosition
	newCast.InitVelocity = initVelocity
	newCast.InitPosition = initPosition
	newCast.LifeTime = 0
	newCast.StartTime = os.clock()
	
	debug.profileend()
	return newCast
end

NOTE 2: This issue usually happens when i tried to throw something at the wall, on the baseplate it seems fine, sometimes it gets stuck sometimes it doesnt ¯_(ツ)_/¯

Any help is appreciated, I’ve been stuck on this for 2 days.

1 Like

Are you firing the projectile from the client of the person who is shooting it first and then send it to the other clients or do you just pile all of the players together after the server processes it and send it to all of them collectively?

I haven’t used SimpleCast before, but narrowing down the source of the problem by starting from the beginning might help.

1 Like

I’m firing a signal from the client to the server, the server handles everything and i fire a remote to all clients where it visualizes this cast across all clients, it does replicate the part correctly, every client can see it, it just gets stuck sometimes.

Assuming SimpleCast gives you some sort of API to handle the impact of a projectile, have you tried printing there to see if it thinks that it already hit something… maybe the client’s character?

As you see, im doing a raycast on the server when receiving the client’s signal, it does print the part in fact, if i were to shoot towards a wall, it would print the wall because it actually hit it, but it would not visualize the projectile correctly across all clients.

Right, but that’s on the server. That part of the code does nothing in reality regarding the actual on-screen result of firing the projectile for each client. I was referring to the impact of the actual projectile based on the fact that SimpleCast module itself tells you what your projectile hit on the client, not your own debugging raycast on the server.

I don’t see any type of RaycastParams set for the client, that may be something to consider.

I do have a raycast params on the client

local CastParams = RaycastParams.new()
CastParams.FilterType = Enum.RaycastFilterType.Exclude
CastParams.FilterDescendantsInstances = { Character }

local Caster = SimpleCast.new(Projectile, {
	MaxLifeTime = 5,
	CacheSize = 25,
	CacheGrowthSize = 10,
	Gravity = Vector3.new(0, -50, 0),
	RaycastParam = CastParams,

	OnCastHit = function()

	end,

	OnCastMove = nil,
	OnCastTerminate = nil
})

Right, but that only applies for the character of the current client. I think if you were to print anything to debug the impact of a projectile on the client, you may see that it is hitting the character when it spawns. No guarantees though.

1 Like

I mean you might be right… i turned CanQuery for every child in the Character and i guess it’s fixed?

I see, I wasn’t entirely sure because generally raycasts generally do not actually detect a collision from the part in which they spawn, so it might be hitting another object inside of your character. Can’t be sure till you test it in the OnCastHit function in your cast data.

Also, I noticed that you cache the player’s character once which is when they first load and spawn in, this means that after they die / respawn, this character will no longer exist and the RaycastParams you used will be rendered useless, likely resulting in the client that shot the projectile having it stuck on their screen too after they respawn.

A quick fix would be passing the character of the player who initially fired the projectile to your remote which fires to all the clients in the server, then you can modify the filter each time a new projectile is fired off.

Also, I noticed that you cache the player’s character once which is when they first load and spawn in, this means that after they die / respawn, this character will no longer exist and the RaycastParams you used will be rendered useless, likely resulting in the client that shot the projectile having it stuck on their screen too after they respawn.

You’re right, i notice hell lots of problems when i reset in general, thanks for pointing it out, will fix.

A quick fix would be passing the character of the player who initially fired the projectile to your remote which fires to all the clients in the server, then you can modify the filter each time a new projectile is fired off.

Thanks, will try it tommorow.

Can’t be sure till you test it in the OnCastHit function in your cast data.

I tested it, seems like several descendats of the Character we’re not allowing this because it technically “hit it” aleardy.

1 Like

I will mark this as solution because it gave me the base.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.