In this tutorial, you'll learn how to cast a laser from the blaster in Creating Player Tools and detect whether or not it hits a player.
Raycasting to Find Collisions
Raycasting creates an invisible ray from a start position towards a given direction with a defined length. If the ray collides with objects or terrain in its path, it will return information on the collision such as the position and the object it collided with.
Finding the Mouse Location
Before a laser can be shot, you must first know where the player is aiming. This can be found by raycasting from the player's 2D mouse location on the screen directly forwards from the camera into the game world. The ray will collide with whatever the player is aiming at with the mouse.
Open the ToolController script inside the Blaster tool from Creating Player Tools. If you haven't completed that tutorial yet you can download the Blaster model and insert it into StarterPack.
At the top of the script, declare a constant named MAX_MOUSE_DISTANCE with a value of 1000.
Create a function called getWorldMousePosition.
local tool = script.Parentlocal MAX_MOUSE_DISTANCE = 1000local function getWorldMousePosition()endlocal function toolEquipped()tool.Handle.Equip:Play()endlocal function toolActivated()tool.Handle.Activate:Play()end-- Connect events to appropriate functionstool.Equipped:Connect(toolEquipped)tool.Activated:Connect(toolActivated)Use the GetMouseLocation function of UserInputService to get the player's 2D mouse location on the screen. Assign this to a variable named mouseLocation.
local UserInputService = game:GetService("UserInputService")local tool = script.Parentlocal MAX_MOUSE_DISTANCE = 1000local function getWorldMousePosition()local mouseLocation = UserInputService:GetMouseLocation()end
Now the 2D mouse location is known, its X and Y properties can be used as parameters for the Camera:ViewportPointToRay() function, which creates a Ray from the screen into the 3D game world.
Use the X and Y properties of mouseLocation as arguments for the ViewportPointToRay() function. Assign this to a variable named screenToWorldRay.
local function getWorldMousePosition()local mouseLocation = UserInputService:GetMouseLocation()-- Create a ray from the 2D mouse locationlocal screenToWorldRay = workspace.CurrentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)end
It's time to use the Raycast function to check if the ray hits an object. This requires a start position and direction vector: in this example, you will use the origin and direction properties of screenToWorldRay.
The length of the direction vector determines how far the ray will travel. The ray needs to be as long as the MAX_MOUSE_DISTANCE, so you'll have to multiply the direction vector by MAX_MOUSE_DISTANCE.
Declare a variable named directionVector and assign it the value of screenToWorldRay.Direction multiplied by MAX_MOUSE_DISTANCE.
local function getWorldMousePosition()local mouseLocation = UserInputService:GetMouseLocation()-- Create a ray from the 2D mouseLocationlocal screenToWorldRay = workspace.CurrentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)-- The unit direction vector of the ray multiplied by a maximum distancelocal directionVector = screenToWorldRay.Direction * MAX_MOUSE_DISTANCECall the Raycast function of workspace, passing the Origin property of screenToWorldRay as the first argument and directionVector as the second. Assign this to a variable named raycastResult.
local function getWorldMousePosition()local mouseLocation = UserInputService:GetMouseLocation()-- Create a ray from the 2D mouseLocationlocal screenToWorldRay = workspace.CurrentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)-- The unit direction vector of the ray multiplied by a maximum distancelocal directionVector = screenToWorldRay.Direction * MAX_MOUSE_DISTANCE-- Raycast from the ray's origin towards its directionlocal raycastResult = workspace:Raycast(screenToWorldRay.Origin, directionVector)
Collision Information
If the raycast operation finds an object hit by the ray, it will return a RaycastResult, which contains information about the collision between the ray and object.
RaycastResult Property | Description |
---|---|
Instance | The BasePart or Terrain cell that the ray intersected. |
Position | Where the intersection occurred; usually a point directly on the surface of a part or terrain. |
Material | The material at the collision point. |
Normal | The normal vector of the intersected face. This can be used to determine which way the face is pointing. |
The Position property will be the position of the object that the mouse is hovering over. If the mouse isn't hovering over any object within a distance of MAX_MOUSE_DISTANCE, raycastResult will be nil.
Create an if statement to check whether raycastResult exists.
If raycastResult has a value, return its Position property.
If raycastResult is nil then find the end of the raycast. Calculate the 3D position of the mouse by adding screenToWorldRay.Origin and directionVector together.
local function getWorldMousePosition()
local mouseLocation = UserInputService:GetMouseLocation()
-- Create a ray from the 2D mouseLocation
local screenToWorldRay = workspace.CurrentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
-- The unit direction vector of the ray multiplied by a maximum distance
local directionVector = screenToWorldRay.Direction * MAX_MOUSE_DISTANCE
-- Raycast from the ray's origin towards its direction
local raycastResult = workspace:Raycast(screenToWorldRay.Origin, directionVector)
if raycastResult then
-- Return the 3D point of intersection
return raycastResult.Position
else
-- No object was hit so calculate the position at the end of the ray
return screenToWorldRay.Origin + directionVector
end
end
Firing Towards the Target
Now that the 3D mouse position is known, it can be used as a target position to fire a laser towards. A second ray can be cast between the player's weapon and the target position using the Raycast function.
Declare a constant called MAX_LASER_DISTANCE at the top of the script and assign it to 500, or your chosen range for the laser blaster.
local UserInputService = game:GetService("UserInputService")local tool = script.Parentlocal MAX_MOUSE_DISTANCE = 1000local MAX_LASER_DISTANCE = 500Create a function called fireWeapon under the getWorldMousePosition function.
Call getWorldMousePosition and assign the result to a variable named mousePosition. This will be the target position for raycast.
-- No object was hit so calculate the position at the end of the rayreturn screenToWorldRay.Origin + directionVectorendendlocal function fireWeapon()local mouseLocation = getWorldMousePosition()endlocal function toolEquipped()tool.Handle.Equip:Play()end
This time, the direction vector for the raycast function will represent the direction from the player's tool position to the target location.
Declare a variable named targetDirection and calculate the direction vector by subtracting the tool position from mouseLocation.
Normalise the vector by using its Unit property. This gives it a magnitude of 1, which makes it easy to multiply by a length later.
local function fireWeapon()local mouseLocation = getWorldMousePosition()-- Calculate a normalised direction vector and multiply by laser distancelocal targetDirection = (mouseLocation - tool.Handle.Position).UnitendDeclare a variable named directionVector and assign to it the targetDirection multiplied by the MAX_LASER_DISTANCE.
local targetDirection = (mouseLocation - tool.Handle.Position).Unit-- The direction to fire the weapon, multiplied by a maximum distancelocal directionVector = targetDirection * MAX_LASER_DISTANCEend
A RaycastParams object can be used to store additional parameters for the raycast function. It will be used in your laser blaster to make sure the raycast doesn't accidentally collide with the player firing the weapon. Any parts included in the FilterDescendantsInstances property of a RaycastParams object will be ignored in the raycast.
Continue the fireWeapon function and declare a variable called weaponRaycastParams. Assign a new RaycastParams object to it.
Create a table containing the player's local character and assign it to the weaponRaycastParams.FilterDescendantsInstances property.
Raycast from the player's tool handle position, in a direction towards the directionVector. Remember to add weaponRaycastParams as an argument this time. Assign this to a variable named weaponRaycastResult.
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local tool = script.Parent
local MAX_MOUSE_DISTANCE = 1000
local MAX_LASER_DISTANCE = 500
local function getWorldMousePosition()
local function fireWeapon()
local mouseLocation = getWorldMousePosition()
-- Calculate a normalised direction vector and multiply by laser distance
local targetDirection = (mouseLocation - tool.Handle.Position).Unit
-- The direction to fire the weapon multiplied by a maximum distance
local directionVector = targetDirection * MAX_LASER_DISTANCE
-- Ignore the player's character to prevent them from damaging themselves
local weaponRaycastParams = RaycastParams.new()
weaponRaycastParams.FilterDescendantsInstances = {Players.LocalPlayer.Character}
local weaponRaycastResult = workspace:Raycast(tool.Handle.Position, directionVector, weaponRaycastParams)
end
Finally, you'll need to check that the raycast operation returned a value. If a value is returned, an object was hit by the ray and a laser can be created between the weapon and hit location. If nothing was returned, the final position needs to be calculated in order to create the laser.
Declare an empty variable named hitPosition.
Use an if statement to check whether weaponRaycastResult has a value. If an object was hit, assign weaponRaycastResult.Position to hitPosition.
local weaponRaycastResult = workspace:Raycast(tool.Handle.Position, directionVector, weaponRaycastParams)-- Check if any objects were hit between the start and end positionlocal hitPositionif weaponRaycastResult thenhitPosition = weaponRaycastResult.PositionendIf weaponRaycastResult has no value, calculate the end position of the raycast by adding together the position of the tool handle with the directionVector. Assign this to hitPosition.
local weaponRaycastResult = workspace:Raycast(tool.Handle.Position, directionVector, weaponRaycastParams)-- Check if any objects were hit between the start and end positionlocal hitPositionif weaponRaycastResult thenhitPosition = weaponRaycastResult.Positionelse-- Calculate the end position based on maximum laser distancehitPosition = tool.Handle.Position + directionVectorendendNavigate to the toolActivated function and call the fireWeapon function so that the laser fires each time the tool is activated.
local function toolActivated()tool.Handle.Activate:Play()fireWeapon()end
Checking the Object Hit
To find if the object hit by the laser is part of a player's character or just a piece of scenery, you'll need to look for a Humanoid, as every character has one.
First, you'll need to find the character model. If a part of the character was hit, you cannot assume the parent of the object hit would be the character. The laser could have hit a body part, an accessory, or a tool, all of which are located in different parts of the character's hierarchy.
You can use FindFirstAncestorOfClass to find a character model ancestor of the object hit by the laser, if one exists. If you find a model and it contains a humanoid, in most cases you can assume it's a character.
Add the highlighted code below to the weaponRaycastResult if statement to check if a character was hit.
-- Check if any objects were hit between the start and end positionlocal hitPositionif weaponRaycastResult thenhitPosition = weaponRaycastResult.Position-- The instance hit will be a child of a character model-- If a humanoid is found in the model then it's likely a player's characterlocal characterModel = weaponRaycastResult.Instance:FindFirstAncestorOfClass("Model")if characterModel thenlocal humanoid = characterModel:FindFirstChildWhichIsA("Humanoid")if humanoid thenprint("Player hit")endendelse-- Calculate the end position based on maximum laser distancehitPosition = tool.Handle.Position + directionVectorend
Now the laser blaster should print Player hit to the output window every time the raycast operation hits another player.
Testing with Multiple Players
Two players are needed to test if the weapon raycast is finding other players, so you need to start a local server.
Select the Test tab in Studio.
Make sure the players dropdown is set to '2 Players' and click the Start button to start a local server with 2 clients. Three windows will appear. The first window will be the local server, the other windows will be the clients for Player1 and Player2.
On one client, test shooting the other player with the weapon by clicking on them. "Player hit" should be displayed in the output each time a player is shot.
You can find out more about the Test tab here.
Finding the Laser Position
The blaster should fire a red beam of light at its target. The function for this will be inside a ModuleScript so it can be reused in other scripts later on. First, the script will need to find the position where the laser beam should be rendered.
Create a ModuleScript named LaserRenderer, parented to StarterPlayerScripts under StarterPlayer.
Open the script and rename the module table to the name of the script LaserRenderer.
Declare a variable named SHOT_DURATION with a value of 0.15. This will be the amount of time (in seconds) the laser is visible for.
Create a function of LaserRenderer named createLaser with two parameters called toolHandle and endPosition.
local LaserRenderer = {}local SHOT_DURATION = 0.15 -- Time that the laser is visible for-- Create a laser beam from a start position towards an end positionfunction LaserRenderer.createLaser(toolHandle, endPosition)endreturn LaserRendererDeclare a variable named startPosition and set the Position property of toolHandle as its value. This will be the position of the player's laser blaster.
Declare a variable named laserDistance and subtract endPosition from startPosition to find the difference between the two vectors. Use the Magnitude property of this to get the length of the laser beam.
function LaserRenderer.createLaser(toolHandle, endPosition)local startPosition = toolHandle.Positionlocal laserDistance = (startPosition - endPosition).MagnitudeendDeclare a laserCFrame variable to store the position and orientation of the laser beam. The position needs to be the midpoint of the start and end of the beam. Use CFrame.lookAt to create a new CFrame located at startPosition and facing towards endPosition. Multiply this by a new CFrame with a Z axis value of half of negative laserDistance to get the midpoint.
function LaserRenderer.createLaser(toolHandle, endPosition)local startPosition = toolHandle.Positionlocal laserDistance = (startPosition - endPosition).Magnitudelocal laserCFrame = CFrame.lookAt(startPosition, endPosition) * CFrame.new(0, 0, -laserDistance / 2)end
Creating the Laser Part
Now that you know where to create a laser beam, you need to add the beam itself. This can be done easily with a Neon part.
Declare a variable laserPart and assign to it a new Part instance.
Set the following properties of laserPart:
- Size: Vector3.new(0.2, 0.2, laserDistance)
- CFrame: laserCFrame
- Anchored: true
- CanCollide: false
- Color: Color3.fromRGB(225, 0, 0) (a strong red color)
- Material: Enum.Material.Neon
Parent laserPart to Workspace.
Add the part to the Debris service so that it gets removed after the amount of seconds in the SHOT_DURATION variable.
function LaserRenderer.createLaser(toolHandle, endPosition)local startPosition = toolHandle.Positionlocal laserDistance = (startPosition - endPosition).Magnitudelocal laserCFrame = CFrame.lookAt(startPosition, endPosition) * CFrame.new(0, 0, -laserDistance / 2)local laserPart = Instance.new("Part")laserPart.Size = Vector3.new(0.2, 0.2, laserDistance)laserPart.CFrame = laserCFramelaserPart.Anchored = truelaserPart.CanCollide = falselaserPart.Color = Color3.fromRGB(225, 0, 0)laserPart.Material = Enum.Material.NeonlaserPart.Parent = workspace-- Add laser beam to the Debris service to be removed & cleaned upDebris:AddItem(laserPart, SHOT_DURATION)end
Now the function to render the laser beam is complete, it can be called by the ToolController.
At the top of the ToolController script, declare a variable named LaserRenderer and require the LaserRenderer ModuleScript located in PlayerScripts.
local UserInputService = game:GetService("UserInputService")local Players = game:GetService("Players")local LaserRenderer = require(Players.LocalPlayer.PlayerScripts.LaserRenderer)local tool = script.ParentAt the bottom of the fireWeapon function, call the LaserRenderer createLaser function using the tool handle and hitPosition as arguments.
-- Calculate the end position based on maximum laser distancehitPosition = tool.Handle.Position + directionVectorendLaserRenderer.createLaser(tool.Handle, hitPosition)endTest the weapon by clicking the Play button. A laser beam should be visible between the weapon and the mouse when the tool is activated.
Controlling Weapon Fire Rate
Weapons need a delay between each shot to stop players from dealing too much damage in a short amount of time. This can be controlled by checking if enough time has passed since a player last fired.
Declare a variable at the top of the ToolController called FIRE_RATE. This will be the minimum time between each shot. Give it a value of your choosing; this example uses 0.3 seconds.
Declare another variable underneath called timeOfPreviousShot with a value of 0. This stores the last time the player fired and will be updated with each shot.
local MAX_MOUSE_DISTANCE = 1000local MAX_LASER_DISTANCE = 300local FIRE_RATE = 0.3local timeOfPreviousShot = 0Create a function called canShootWeapon with no parameters. This function will look at how much time has passed since the previous shot and return true or false.
local FIRE_RATE = 0.3local timeOfPreviousShot = 0-- Check if enough time has passed since previous shot was firedlocal function canShootWeapon()endlocal function getWorldMousePosition()Inside the function declare a variable named currentTime; assign to it the result of calling the tick() function. This returns how much time has elapsed, in seconds, since the 1st of January 1970 (an arbitrary date widely used to calculate time).
Subtract the timeOfPreviousShot from currentTime and return false if the result is smaller than FIRE_RATE; otherwise, return true.
-- Check if enough time has passed since previous shot was firedlocal function canShootWeapon()local currentTime = tick()if currentTime - timeOfPreviousShot < FIRE_RATE thenreturn falseendreturn trueendAt the end of the fireWeapon function, update timeOfPreviousShot each time the weapon is fired using tick.
hitPosition = tool.Handle.Position + directionVectorendtimeOfPreviousShot = tick()LaserRenderer.createLaser(tool.Handle, hitPosition)endInside the toolActivated function, create an if statement and call canShootWeapon to check if the weapon can be fired.
local function toolActivated()if canShootWeapon() thentool.Handle.Activate:Play()fireWeapon()endend
When you test the blaster you should find that no matter how fast you click, there will always be a short 0.3 seconds delay between each shot.
Damaging the Player
Clients cannot damage other clients directly; the server needs to be responsible for issuing damage when a player is hit.
Clients can use a RemoteEvent to tell the server that a character has been hit. These should be stored in ReplicatedStorage, where they are visible to both client and server.
Create a Folder in ReplicatedStorage named Events.
Insert a RemoteEvent into the Events folder and name it DamageCharacter.
In ToolController, create variables at the start of the script for ReplicatedStorage and the Events folder.
local UserInputService = game:GetService("UserInputService")local Players = game:GetService("Players")local ReplicatedStorage = game:GetService("ReplicatedStorage")local LaserRenderer = require(Players.LocalPlayer.PlayerScripts.LaserRenderer)local tool = script.Parentlocal eventsFolder = ReplicatedStorage.Eventslocal MAX_MOUSE_DISTANCE = 1000local MAX_LASER_DISTANCE = 500Replace the "Player hit" print statement in fireWeapon with a line of Lua to fire the DamageCharacter remote event with the characterModel variable as an argument.
local characterModel = weaponRaycastResult.Instance:FindFirstAncestorOfClass("Model")if characterModel thenlocal humanoid = characterModel:FindFirstChildWhichIsA("Humanoid")if humanoid theneventsFolder.DamageCharacter:FireServer(characterModel)endendelse-- Calculate the end position based on maximum laser distancehitPosition = tool.Handle.Position + directionVectorend
The server needs to deal damage to the player who has been hit when the event is fired.
Insert a Script into ServerScriptService and name it ServerLaserManager.
Declare a variable named LASER_DAMAGE and set it to 10, or a value of your choice.
Create a function named damageCharacter with two parameters called playerFired and characterToDamage.
Inside the function, find the character's Humanoid and subtract LASER_DAMAGE from its health.
Connect the damageCharacter function to the DamageCharacter remote event in the Events folder.
local ReplicatedStorage = game:GetService("ReplicatedStorage")local eventsFolder = ReplicatedStorage.Eventslocal LASER_DAMAGE = 10function damageCharacter(playerFired, characterToDamage)local humanoid = characterToDamage:FindFirstChildWhichIsA("Humanoid")if humanoid then-- Remove health from characterhumanoid.Health -= LASER_DAMAGEendend-- Connect events to appropriate functionseventsFolder.DamageCharacter.OnServerEvent:Connect(damageCharacter)Test the blaster with 2 players by starting a local server. When you shoot the other player, their health will decrease by the number assigned to LASER_DAMAGE.
Rendering Other Player's Laser Beams
Currently, the laser beam is created by the client firing the weapon, so only they will be able to see the laser beam.
If the laser beam was created on the server then everyone would be able to see it. However, there would be a small delay between the client shooting the weapon and the server receiving the information about the shot. This would mean the client shooting the weapon would see a delay between when they activate the weapon and when they see the laser beam; the weapon would feel laggy as a result.
To solve this issue, every client will create their own laser beams. This means the client shooting the weapon will see the laser beam instantly. Other clients will experience a small delay between when another player shoots and a beam appears. This is the best case scenario: there is no way to communicate one client's laser to other clients any faster.
Shooter's Client
First, the client needs to tell the server it has fired a laser and provide the end position.
Insert a RemoteEvent into the Events folder in ReplicatedStorage and name it LaserFired.
Locate the fireWeapon function in the ToolController script. At the end of the function, fire the LaserFired remote event using hitPosition as an argument.
hitPosition = tool.Handle.Position + directionVectorendtimeOfPreviousShot = tick()eventsFolder.LaserFired:FireServer(hitPosition)LaserRenderer.createLaser(tool.Handle, hitPosition)end
The Server
The server now must receive the event that the client has fired and tell all clients the start and end position of the laser beam so they can also render it.
In the ServerLaserManager script, create a function named playerFiredLaser above damageCharacter with two parameters called playerFired and endPosition.
Connect the function to the LaserFired remote event.
-- Notify all clients that a laser has been fired so they can display the laserlocal function playerFiredLaser(playerFired, endPosition)end-- Connect events to appropriate functionseventsFolder.DamageCharacter.OnServerEvent:Connect(damageCharacter)eventsFolder.LaserFired.OnServerEvent:Connect(playerFiredLaser)
The server needs the start position of the laser. This could be sent from the client, but it's best to avoid trusting the client where possible. The character's weapon handle position will be the start position, so the server can find it from there.
Create a function getPlayerToolHandle above the playerFiredLaser function with a parameter called player.
Use the following code to search the player's character for the weapon and return the handle object.
local LASER_DAMAGE = 10-- Find the handle of the tool the player is holdinglocal function getPlayerToolHandle(player)local weapon = player.Character:FindFirstChildOfClass("Tool")if weapon thenreturn weapon:FindFirstChild("Handle")endend-- Notify all clients that a laser has been fired so they can display the laserlocal function playerFiredLaser(playerFired, endPosition)
The server can now call FireAllClients on the LaserFired remote event to send the information required to render the laser to the clients. This includes the player who fired the laser (so the client for that player does not render the laser twice), the handle of the blaster (which acts as a starting position for the laser) and the end position of the laser.
In the playerFiredLaser function, call the getPlayerToolHandle function with playerFired as an argument and assign the value to a variable named toolHandle.
If toolHandle exists, fire the LaserFired event for all clients using playerFired, toolHandle and endPosition as arguments.
-- Notify all clients that a laser has been fired so they can display the laserlocal function playerFiredLaser(playerFired, endPosition)local toolHandle = getPlayerToolHandle(playerFired)if toolHandle theneventsFolder.LaserFired:FireAllClients(playerFired, toolHandle, endPosition)endend
Rendering on the Clients
Now FireAllClients has been called, each client will receive an event from the server to render a laser beam. Each client can reuse the LaserRenderer module from earlier to render the laser beam using the tool's handle position and end position value sent by the server. The player that fired the laser beam in the first place should ignore this event otherwise they'll see 2 lasers.
Create a LocalScript in StarterPlayerScripts called ClientLaserManager.
Inside the script, require the LaserRenderer module.
Create a function named createPlayerLaser with the parameters playerWhoShot, toolHandle and endPosition.
Connect the function to the LaserFired remote event in the Events folder.
In the function, use an if statement to check if playerWhoShot does not equal the LocalPlayer.
Inside the if statement, call the createLaser function from the LaserRenderer module using toolHandle and endPosition as arguments.
local Players = game:GetService("Players")local ReplicatedStorage = game:GetService("ReplicatedStorage")local LaserRenderer = require(script.Parent:WaitForChild("LaserRenderer"))local eventsFolder = ReplicatedStorage.Events-- Display another player's laserlocal function createPlayerLaser(playerWhoShot, toolHandle, endPosition)if playerWhoShot ~= Players.LocalPlayer thenLaserRenderer.createLaser(toolHandle, endPosition)endendeventsFolder.LaserFired.OnClientEvent:Connect(createPlayerLaser)Test the blaster with 2 players by starting a local server. Position each client on different sides of your monitor so you can see both windows at once. When you shoot on one client you should see the laser on the other client.
Sound Effects
The shooting sound effect currently only plays on the client that's shooting the projectile. You'll need to move the code to play the sound so that other players will hear it too.
In the ToolController script, navigate to the toolActivated function and remove the line which plays the Activate sound.
local function toolActivated()if canShootWeapon() thenfireWeapon()endendAt the bottom of the createLaser function in LaserRenderer, declare a variable named shootingSound and use the FindFirstChild() method of toolHandle to check for the Activate sound.
Use an if statement to check if shootingSound exists; if it does, call its Play function.
laserPart.Parent = workspace-- Add laser beam to the Debris service to be removed & cleaned upDebris:AddItem(laserPart, SHOT_DURATION)-- Play the weapon's shooting soundlocal shootingSound = toolHandle:FindFirstChild("Activate")if shootingSound thenshootingSound:Play()endend
Securing Remotes using Validation
If the server isn't checking data from incoming requests, a hacker can abuse remote functions and events and use them to send fake values to the server. It's important to use server-side validation to prevent this.
In its current form, the DamageCharacter remote event is very vulnerable to attack. Hackers could use this event to damage any player they want in the game without shooting them.
Validation is the process of checking that the values being sent to the server are realistic. In this case, the server will need to:
- Check if the distance between the player and the position hit by the laser is within a certain boundary.
- Raycast between the weapon that fired the laser and the hit position to make sure the shot was possible and didn't go through any walls.
Client
The client needs to send the server the position hit by the raycast so it can check the distance is realistic.
In ToolController, navigate to the line where the DamageCharacter remote event is fired in the fireWeapon function.
Add hitPosition as an argument.
if characterModel thenlocal humanoid = characterModel:FindFirstChildWhichIsA("Humanoid")if humanoid theneventsFolder.DamageCharacter:FireServer(characterModel, hitPosition)endend
Server
The client is now sending an extra parameter through the DamageCharacter remote event, so the ServerLaserManager needs to be adjusted to accept it.
In the ServerLaserManager script, add a hitPosition parameter to the damageCharacter function.
function damageCharacter(playerFired, characterToDamage, hitPosition)local humanoid = characterToDamage:FindFirstChildWhichIsA("Humanoid")if humanoid then-- Remove health from characterhumanoid.Health -= LASER_DAMAGEendendBelow the getPlayerToolHandle function, create a function named isHitValid with three parameters: playerFired, characterToDamage and hitPosition.
endlocal function isHitValid(playerFired, characterToDamage, hitPosition)end
The first check will be the distance between the hit position and the character hit.
Declare a variable named MAX_HIT_PROXIMITY at the top of the script and assign it a value of 10. This will be the maximum distance allowed between the hit and character. A tolerance is needed because the character may have moved slightly since the client fired the event.
local ReplicatedStorage = game:GetService("ReplicatedStorage")local eventsFolder = ReplicatedStorage.Eventslocal LASER_DAMAGE = 10local MAX_HIT_PROXIMITY = 10In the isHitValid function, calculate the distance between the character and the hit position. If the distance is larger than MAX_HIT_PROXIMITY then return false.
local function isHitValid(playerFired, characterToDamage, hitPosition)-- Validate distance between the character hit and the hit positionlocal characterHitProximity = (characterToDamage.HumanoidRootPart.Position - hitPosition).Magnitudeif characterHitProximity > MAX_HIT_PROXIMITY thenreturn falseendend
The second check will involve a raycast between the weapon fired and the hit position. If the raycast returns an object that isn't the character, you can assume the shot wasn't valid since something was blocking the shot.
Copy the code below to perform this check. Return true at the end of the function: if it reaches the end, all checks have passed.
local function isHitValid(playerFired, characterToDamage, hitPosition)-- Validate distance between the character hit and the hit positionlocal characterHitProximity = (characterToDamage.HumanoidRootPart.Position - hitPosition).Magnitudeif characterHitProximity > 10 thenreturn falseend-- Check if shooting through wallslocal toolHandle = getPlayerToolHandle(playerFired)if toolHandle thenlocal rayLength = (hitPosition - toolHandle.Position).Magnitudelocal rayDirection = (hitPosition - toolHandle.Position).Unitlocal raycastParams = RaycastParams.new()raycastParams.FilterDescendantsInstances = {playerFired.Character}local rayResult = workspace:Raycast(toolHandle.Position, rayDirection * rayLength, raycastParams)-- If an instance was hit that was not the character then ignore the shotif rayResult and not rayResult.Instance:IsDescendantOf(characterToDamage) thenreturn falseendendreturn trueendDeclare a variable in the damageCharacter function called validShot. Assign to it the result of a call to the isHitValid function with three arguments: playerFired, characterToDamage and hitPosition.
In the below if statement, add an and operator to check if validShot is true.
function damageCharacter(playerFired, characterToDamage, hitPosition)local humanoid = characterToDamage:FindFirstChildWhichIsA("Humanoid")local validShot = isHitValid(playerFired, characterToDamage, hitPosition)if humanoid and validShot then-- Remove health from characterhumanoid.Health -= LASER_DAMAGEendend
Now the damageCharacter remote event is more secure and will prevent most players from abusing it. Note that some malicious players will often find ways around validation; keeping remote events secure is a continuous effort.
Your laser blaster is now complete, with a basic hit detection system using raycasting. Try the Detecting User Input tutorial to find out how you can add a reloading action to your laser blaster, or create a fun game map and try your laser blaster out with other players!
Final Code
ToolController
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local LaserRenderer = require(Players.LocalPlayer.PlayerScripts.LaserRenderer)
local tool = script.Parent
local eventsFolder = ReplicatedStorage.Events
local MAX_MOUSE_DISTANCE = 1000
local MAX_LASER_DISTANCE = 500
local FIRE_RATE = 0.3
local timeOfPreviousShot = 0
-- Check if enough time has passed since previous shot was fired
local function canShootWeapon()
local currentTime = tick()
if currentTime - timeOfPreviousShot < FIRE_RATE then
return false
end
return true
end
local function getWorldMousePosition()
local mouseLocation = UserInputService:GetMouseLocation()
-- Create a ray from the 2D mouse location
local screenToWorldRay = workspace.CurrentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
-- The unit direction vector of the ray multiplied by a maximum distance
local directionVector = screenToWorldRay.Direction * MAX_MOUSE_DISTANCE
-- Raycast from the roy's origin towards its direction
local raycastResult = workspace:Raycast(screenToWorldRay.Origin, directionVector)
if raycastResult then
-- Return the 3D point of intersection
return raycastResult.Position
else
-- No object was hit so calculate the position at the end of the ray
return screenToWorldRay.Origin + directionVector
end
end
local function fireWeapon()
local mouseLocation = getWorldMousePosition()
-- Calculate a normalised direction vector and multiply by laser distance
local targetDirection = (mouseLocation - tool.Handle.Position).Unit
-- The direction to fire the weapon, multiplied by a maximum distance
local directionVector = targetDirection * MAX_LASER_DISTANCE
-- Ignore the player's character to prevent them from damaging themselves
local weaponRaycastParams = RaycastParams.new()
weaponRaycastParams.FilterDescendantsInstances = {Players.LocalPlayer.Character}
local weaponRaycastResult = workspace:Raycast(tool.Handle.Position, directionVector, weaponRaycastParams)
-- Check if any objects were hit between the start and end position
local hitPosition
if weaponRaycastResult then
hitPosition = weaponRaycastResult.Position
-- The instance hit will be a child of a character model
-- If a humanoid is found in the model then it's likely a player's character
local characterModel = weaponRaycastResult.Instance:FindFirstAncestorOfClass("Model")
if characterModel then
local humanoid = characterModel:FindFirstChildWhichIsA("Humanoid")
if humanoid then
eventsFolder.DamageCharacter:FireServer(characterModel, hitPosition)
end
end
else
-- Calculate the end position based on maximum laser distance
hitPosition = tool.Handle.Position + directionVector
end
timeOfPreviousShot = tick()
eventsFolder.LaserFired:FireServer(hitPosition)
LaserRenderer.createLaser(tool.Handle, hitPosition)
end
local function toolEquipped()
tool.Handle.Equip:Play()
end
local function toolActivated()
if canShootWeapon() then
fireWeapon()
end
end
tool.Equipped:Connect(toolEquipped)
tool.Activated:Connect(toolActivated)
LaserRenderer
local LaserRenderer = {}
local Debris = game:GetService("Debris")
local SHOT_DURATION = 0.15 -- Time that the laser is visible for
-- Create a laser beam from a start position towards an end position
function LaserRenderer.createLaser(toolHandle, endPosition)
local startPosition = toolHandle.Position
local laserDistance = (startPosition - endPosition).Magnitude
local laserCFrame = CFrame.lookAt(startPosition, endPosition) * CFrame.new(0, 0, -laserDistance / 2)
local laserPart = Instance.new("Part")
laserPart.Size = Vector3.new(0.2, 0.2, laserDistance)
laserPart.CFrame = laserCFrame
laserPart.Anchored = true
laserPart.CanCollide = false
laserPart.Color = Color3.fromRGB(255, 0, 0)
laserPart.Material = Enum.Material.Neon
laserPart.Parent = workspace
-- Add laser beam to the Debris service to be removed & cleaned up
Debris:AddItem(laserPart, SHOT_DURATION)
-- Play the weapon's shooting sound
local shootingSound = toolHandle:FindFirstChild("Activate")
if shootingSound then
shootingSound:Play()
end
end
return LaserRenderer
ServerLaserManager
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local eventsFolder = ReplicatedStorage.Events
local LASER_DAMAGE = 10
local MAX_HIT_PROXIMITY = 10
-- Find the handle of the tool the player is holding
local function getPlayerToolHandle(player)
local weapon = player.Character:FindFirstChildOfClass("Tool")
if weapon then
return weapon:FindFirstChild("Handle")
end
end
local function isHitValid(playerFired, characterToDamage, hitPosition)
-- Validate distance between the character hit and the hit position
local characterHitProximity = (characterToDamage.HumanoidRootPart.Position - hitPosition).Magnitude
if characterHitProximity > MAX_HIT_PROXIMITY then
return false
end
-- Check if shooting through walls
local toolHandle = getPlayerToolHandle(playerFired)
if toolHandle then
local rayLength = (hitPosition - toolHandle.Position).Magnitude
local rayDirection = (hitPosition - toolHandle.Position).Unit
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {playerFired.Character}
local rayResult = workspace:Raycast(toolHandle.Position, rayDirection * rayLength, raycastParams)
-- If an instance was hit that was not the character then ignore the shot
if rayResult and not rayResult.Instance:IsDescendantOf(characterToDamage) then
return false
end
end
return true
end
-- Notify all clients that a laser has been fired so they can display the laser
local function playerFiredLaser(playerFired, endPosition)
local toolHandle = getPlayerToolHandle(playerFired)
if toolHandle then
eventsFolder.LaserFired:FireAllClients(playerFired, toolHandle, endPosition)
end
end
function damageCharacter(playerFired, characterToDamage, hitPosition)
local humanoid = characterToDamage:FindFirstChildWhichIsA("Humanoid")
local validShot = isHitValid(playerFired, characterToDamage, hitPosition)
if humanoid and validShot then
-- Remove health from character
humanoid.Health -= LASER_DAMAGE
end
end
-- Connect events to appropriate functions
eventsFolder.DamageCharacter.OnServerEvent:Connect(damageCharacter)
eventsFolder.LaserFired.OnServerEvent:Connect(playerFiredLaser)
ClientLaserManager
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local LaserRenderer = require(Players.LocalPlayer.PlayerScripts:WaitForChild("LaserRenderer"))
local eventsFolder = ReplicatedStorage.Events
-- Display another player's laser
local function createPlayerLaser(playerWhoShot, toolHandle, endPosition)
if playerWhoShot ~= Players.LocalPlayer then
LaserRenderer.createLaser(toolHandle, endPosition)
end
end
eventsFolder.LaserFired.OnClientEvent:Connect(createPlayerLaser)