Alright, so pretty much I’ve been working on this gun tool for my game, and pretty much I want to make it so that whenever my pistol is fired, It sends out a laser that shows what it hit, essentially.
Preferably explain it to me in a way that is more understandable, as I am still kinda beginning to get into the more complicated parts of scripting, like hitboxes and more. So that would be appreciated.
Here is my Local Script for the Pistol:
local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local gui = player.PlayerGui.ScreenGui
local mouse = player:GetMouse()
local tool = script.Parent
local reloadSound = tool.ReloadSound
local cooldownTime = 0.3
local cooldown = false
local ammo = 6
local maxAmmo = 6
local reloading = false
local isEquipped = false
local function reload()
if reloading == false then
reloadSound:Play()
reloading = true
task.wait(1)
ammo = maxAmmo
reloading = false
end
end
tool.Activated:Connect(function(hit)
if cooldown == false and ammo > 0 and reloading == false then
ammo -= 1
cooldown = true
tool.Shoot:FireServer(mouse.Hit.Position)
task.wait(cooldownTime)
cooldown = false
elseif ammo <= 0 and reloading == false then
reload()
end
end)
uis.InputBegan:Connect(function(inpObj, gpe)
if gpe == true then return end
if isEquipped == true then
if inpObj.KeyCode == Enum.KeyCode.R then
reload()
end
end
end)
tool.Equipped:Connect(function(hit)
isEquipped = true
if isEquipped == true then
gui.Enabled = true
end
end)
tool.Unequipped:Connect(function(hit)
isEquipped = false
if isEquipped == false then
gui.Enabled = false
end
end)
while true do
task.wait()
gui.TextLabel.Text = ammo.."/"..maxAmmo
end
Here is my ServerSide script for the pistol:
local event = script.Parent.Shoot
local tool = script.Parent
local shootSound = tool.ShootSound
local ImpactSound = tool.ImpactSound
event.OnServerEvent:Connect(function(player, mousePosition)
shootSound:Play()
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local raycastResult = workspace:Raycast(tool.Handle.Barrel.Position, (mousePosition - tool.Handle.Barrel.Position) * 300, raycastParams)
if raycastResult then
local rayInstance = raycastResult.Instance
local model = rayInstance:FindFirstAncestorOfClass("Model")
if model:FindFirstChild("Humanoid") then
ImpactSound:Play()
model:FindFirstChild("Humanoid").Health -= 15
end
end
end)
What I want:
What I have right now: