For a new game I am working on, there is a turret on the back of a vehicle. Here is a video of it in action.
This looks ok, but functionally, it is far from ideal. The system works by detecting when a player is holding down his or her mouse (or whatever, e.g. tapping / the RightTrigger on Console), sends the mouse data through a RemoteEvent, and then a server script runs a loop that creates a brick as the “bullet” or “bullet trail.” At the end of the bullet trail, an invisible explosion happens, and that is what does the damage on hit players. When the player releases the mouse, the loop stops.
Here is the code:
---server
local system = script.Parent
local gunSeat = system.gunSeat
local core = system.important
local firin = false
local fireRate = system:GetAttribute("firerate") or 850
local damage = system:GetAttribute("damage") or 25
core.firing.OnServerEvent:Connect(function(plr:Player,firing:BoolValue,mouseHit,mouseTarget:Instance)
--print("hi")
firin = firing
while firin and system:GetAttribute("ammo") > 0 do
task.wait()
local barrels = core.barrels:GetChildren()
local choosenBarrel = barrels[math.random(1,#barrels)]
if mouseTarget == nil or system:FindFirstChild("bullet") then return end
if mouseTarget:IsAncestorOf(system.Parent) == false then
core.effect.Fire:Play()
core.effect.Heat.Enabled = true
core.barrelMount.HingeConstraint.AngularVelocity = 15
local theTar = mouseHit.p + Vector3.new(math.random(-5, 5), math.random(-5, 5), math.random(-5, 5))
local bull = Instance.new("Part")
bull.Name = "bullet"
bull.Parent = system
bull.CFrame = CFrame.new((theTar + choosenBarrel.firePoint.WorldCFrame.Position)/2,choosenBarrel.firePoint.WorldCFrame.Position) --- script.Parent.Parent is the vehicle seat
bull.CanCollide = false
bull.BrickColor = BrickColor.new(24)
bull.Anchored = true
bull.Size = Vector3.new(1,1.2,1)
game.Debris:AddItem(bull,0.1)
local mesher = Instance.new("BlockMesh")
mesher.Parent = bull
mesher.Scale = Vector3.new(0.2, 0.2, (mouseHit.p - choosenBarrel.firePoint.WorldCFrame.Position).magnitude)
local ex = Instance.new("Explosion")
ex.Visible = false
ex.BlastRadius = 5
ex.BlastPressure = 0
ex.Position = theTar
ex.Parent = game.Workspace
ex.Hit:Connect(function(hit)
if hit and hit.Parent then
local hum = cf.findHumanoid(hit)
if hum and not hum:FindFirstChild("explosion") then
local tag1 = Instance.new("Folder",hum)
tag1.Name = "explosion"
game:GetService("Debris"):AddItem(tag1,.1)
local tag2 = Instance.new("ObjectValue",hum)
tag2.Value = plr
tag2.Name = "creator"
game:GetService("Debris"):AddItem(tag2,3)
hum:TakeDamage(damage)
end
end
end)
local oldAmmo = system:GetAttribute("ammo")
system:SetAttribute("ammo",oldAmmo-1)
task.wait(60/fireRate)
end
end
core.barrelMount.HingeConstraint.AngularVelocity = 0
core.effect.Heat.Enabled = false
end)
---client
mouse.Button1Down:Connect(function()
if firingGun == false then
firingGun = true
firing:FireServer(true,mouse.Hit,mouse.Target)
end
end)
mouse.Button1Up:Connect(function()
firingGun = false
firing:FireServer(false,mouse.Hit,mouse.Target)
end)
uis.InputEnded:Connect(function(input) ---backup, in case Button1Up fails - actually fixed that
if input.UserInputType == Enum.UserInputType.MouseButton1 then
firingGun = false
firing:FireServer(false,mouse.Hit,mouse.Target)
end
end)
It works, but it is not something I want to keep using, given that you can see in the video, a few things happen:
-
When you move your mouse, the beam does not follow. You have to release your mouse, and then hold down again, for the location to be updated.
-
Is it extremely inaccurate, the beam/bullet part flies around pretty wildly.
What I would like to do, is convert this system over to a raycast based on, like seen here. This would allow for the bullet’s targeting to be updated even when the mouse moves, would allow for better accuracy, and would allow for better hit-detecting that isn’t via an invisible explosion…
From my brief research on raycasting weapons, they need a RemoteEvent to detect hits, which I know is super vulnerable. I want to make something safe and secure from exploiters.
How would I do this?