I have created Raycast weapons for the game I am working on. It functions by the firing the server when the weapon is fired, though I have a major problem. For some reason the Raycast which is casted from the weapon is not detecting hits on a character properly, I am unsure why but have a suspicion that the raycasting is being delayed. Here is a gif of this:
Remote.OnServerEvent:Connect(function(player, muzzlePos, mousePos, Weapon, Head, Damage, bulletSpread, Speed, BulletType)
local Ignore = {player.Character}
table.insert(Ignore, player.Character)
local Spread = bulletSpread * 100
local ray = Ray.new(Head, (mousePos - Head).unit * 4000)
local Part, Position, Vector = game.Workspace:FindPartOnRayWithIgnoreList(ray, Ignore)
local Distance = (muzzlePos - Position).magnitude
if Part then
if Part.Parent:FindFirstChild("Humanoid") then
Part.Parent:FindFirstChild("Humanoid"):TakeDamage(Damage)
elseif Part.Parent:FindFirstChild("Zombie") then
Part.Parent:FindFirstChild("Zombie"):TakeDamage(Damage)
elseif Part.Parent.Parent and Part.Parent.Parent:FindFirstChild("Humanoid") then
Part.Parent.Parent:FindFirstChild("Humanoid"):TakeDamage(Damage)
end
end
Remote:FireAllClients(player, Position, Distance, muzzlePos, Weapon, Head, Speed, BulletType, Part, Vector)
end)
Seem to be missing some of the variables in that raycast from what you sent but I’d recommend trying out the screen point to ray function. It uses raycasting to find the hit position and then you could draw the raycast between the player and that point to check from obstructions. Hope this helps!
You shouldn’t change anything as it actually does what it’s intended to do.
Delayed communication is an issue all games face. Though, games which rely on server-client communication it being projectiles, actions or whatnot have different ways of dealing with this issue.
I’m not the most experienced when it comes to lag compensation but I know the theoretical part behind it. A method a lot of games use is ‘rewinding’ the server back in time based on the client who fired the projectile’s ping. You can do so by ‘recording’ all the character positions in your server, ‘rewinding’ back to roughly when the client locally fired the projectile (roughly represented by the ping) and using this data to simulate where/what the projectile would hit at that time.
Distance in ‘vector language’ is called magnitude. To get the magnitude between two vector positions, you would simply subtract them and read the new vector’s magnitude like so: (p1 - p2).magnitude.
However, I don’t agree with this being a reliable method to counter improper information sent by the client (via exploiting) as magnitude has nothing to do with directions, meaning you could simply tell the server to shoot the guy behind you as you’re looking the other way and the server would just allow it.