Hello!
I am making a game for practice and I have learned ray casting and a bit more!
The weapon works fine, the only problem I’m having is when a player shoots another, the bullet hits the player and sometimes the player doesn’t die, here is a screenshot above:
I have tried looking back through the code and found nothing, aswell as the output not saying anything.
Here is the codes to the gun:
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local mouse = game.Players.LocalPlayer:GetMouse()
local Tool = script.Parentscript.Parent.Equipped:Connect(function(mouse)
mouse.Icon = “http://www.roblox.com/asset/?id=79658449”
end)local Cooldown = false
local COOLDOWN_TIME = 3local function Reload()
script.Parent.Handle.Reloading:Play()
endlocal function Pew()
game.ReplicatedStorage.ShootGun:FireServer(Mouse.hit.p, Tool.Handle)
endlocal function Shoot()
– Function code.if Cooldown == true then
return
endCooldown = true
Pew()wait(1)
Reload()print(“Shoots!”)
wait(COOLDOWN_TIME)
Cooldown = falseend
Tool.Activated:Connect(function()
– This will run whenever someone clicks with the tool in their hand.Shoot()
end)
Here is the second:
local RANGE = 500
local DAMAGE = 100game.ReplicatedStorage.ShootGun.OnServerEvent:Connect(function(Player, TargetLocation, Handle)
if Player.Character == nil then – Make sure their character exists.
return
endHandle.ShootingSound:Play()
local Beam = Instance.new(“Part”, workspace)
Beam.BrickColor = BrickColor.new(“Fossil”)
Beam.FormFactor = “Custom”
Beam.Transparency = 0.55
Beam.CastShadow = false
Beam.Anchored = true
Beam.CanCollide = false– Math.
local Distance = (Handle.Position - TargetLocation).magnitude
Beam.Size = Vector3.new(0.1, 0.1, Distance)
Beam.CFrame = CFrame.new(Handle.Position, TargetLocation) * CFrame.new(0, 0, -Distance/2)game.Debris:AddItem(Beam, 0.1) – Destroy.
–RayCasting [Shoot a ray, get wherever it hit]
local NewRay = RaycastParams.new()
local RayDirection = (TargetLocation - Handle.Position) * RANGE – How far you want the ray to travel before stoppingNewRay.FilterDescendantsInstances = {Player.Character}
local Result = workspace:Raycast(Handle.Position, RayDirection, NewRay)
if Result then – If we got a result back.
if Result.Instance then
– It actually hit something.if Result.Instance.Parent:FindFirstChild("Humanoid") then Result.Instance.Parent.Humanoid.Health -= DAMAGE Handle.HitPlayer:Play() end end
end
end)
I have no idea wether i missed something for the raycast to properly work or wether this is completly normal!
Please help!
Cheers.