Best Way To Make a Projectile?

The question is simple: What is the best way to make a projectile?

The projectile would come from a user and should be replicated to all other players. If the projectile hits someone it should damage them.

A lot of videos and tutorials currently don’t really cover how to do it, commonly making the mistake of creating the projectiles on the server which can cause a delay from the input. I’ve heard people say to run the projectile script on the clients and wait till the owner says they should take damage, but how do you prevent hacking?

It’s easy to create a projectile, but what is the best way to do it?

5 Likes

To create a projectile in Roblox Lua, there are different approaches you can take depending on your game’s requirements. Here’s a general approach that could work for many types of games:

  1. Spawn the projectile on the client that fired it using a LocalScript. This will eliminate any delay caused by network latency.
  2. Use RemoteEvents or RemoteFunctions to replicate the projectile to the server and then to other players. When a player fires a projectile, it should be replicated to the server, and then the server should replicate it to all other players.
  3. When a projectile hits a player, you can use a RemoteEvent to notify the server and damage the player. This will prevent players from hacking by making it impossible to trigger a hit without the server’s confirmation.
  4. To prevent cheating, you can add validation checks to the server that ensure that the projectile’s trajectory and speed are within expected parameters. You can also use anti-exploit measures to detect and prevent malicious behavior.

Here’s an example implementation:

-- LocalScript in the projectile
local Projectile = Instance.new("Part")
-- Set up Projectile properties, such as Size, Color, and Material

-- On Fire:
local projectile = Projectile:Clone()
-- Set up projectile properties, such as Velocity and Anchored
projectile.Parent = workspace

-- Replicate to server
local ReplicateProjectile = game.ReplicatedStorage:WaitForChild("ReplicateProjectile")
ReplicateProjectile:FireServer(projectile)

-- On Hit:
local HitEvent = game.ReplicatedStorage:WaitForChild("HitEvent")
HitEvent:FireServer(hitPlayer)

-- Server Script
local ReplicateProjectile = game.ReplicatedStorage:WaitForChild("ReplicateProjectile")
local HitEvent = game.ReplicatedStorage:WaitForChild("HitEvent")

ReplicateProjectile.OnServerEvent:Connect(function(player, projectile)
    -- Validate the projectile's properties
    if not IsValidProjectile(projectile) then
        return
    end

    -- Replicate the projectile to all other players
    for _, otherPlayer in ipairs(game.Players:GetPlayers()) do
        if otherPlayer ~= player then
            ReplicateProjectile:FireClient(otherPlayer, projectile)
        end
    end
end)

HitEvent.OnServerEvent:Connect(function(player, hitPlayer)
    -- Validate the hit and the players involved
    if not IsValidHit(player, hitPlayer) then
        return
    end

    -- Damage the hit player
    hitPlayer:TakeDamage()
end)

This is just an example implementation, and you’ll likely need to modify it to fit your specific game’s requirements. Remember to test your implementation thoroughly and consider the potential edge cases to prevent exploits and ensure a smooth gameplay experience.

7 Likes

Thank you so much!

I was able to make something like this but I’m a bit confused. How would you validate if they hit on both clients or if they’re correct if there are two separate projectiles for both clients?

Usually how games handle that is doing all the hit detection on the client and then on the server they do some simple validation to make sure the hit is valid

2 Likes

Is the validation just like rechecking the collision of the projectile? How exactly would the validation work?

Validation can be something like checking the coordinates of the projectile, who’s sending it, where they are, how fast it’s going, who it hit, etc. Essentially, just check to see if everything the client sent to the server actually makes sense.

(P.S. the code the other person posted doesn’t function.)

2 Likes

If you recheck the collision, it may or may not hit because of the difference in position. So by validation I mean checking if you have enough ammo, line of sight, making sure the players arent on opposite ends of the map to hit an impossible shot, stuff like that

2 Likes

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