I have created a ray cast gun using the laser gun wiki and added reload functions, animations, ect. But recently I noticed that the enemies would be stuck on 0 or lower health and not dying. I searched it up and one thing said that the lag could be from the fact that it is all client sided and the server doesn't realize the zombie is dead. So I created a new script using remote events, a local script, and a server script, but when I try to shoot it the ray detects the player even though I set it to ignore the player. Please tell me what I did wrong and if I'm using remote events right, just started coding about 2 months ago. The local script is creating the ray and beam is created but no damage is dealt.
Local Script:
--Variables---
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local firePart = script.Parent:WaitForChild("firePart")
local canShoot = true
local UserInputService = game:GetService("UserInputService")
local reloading = false
local reloadNoti = player.PlayerGui.gunGui.Frame.reload
local character = player.Character
local HRP = character.HumanoidRootPart
local light = script.Parent.firePart.SpotLight
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local shotEvent = ReplicatedStorage.Events:WaitForChild("Shot")
local ignoreList = {character}
--Sounds--
local hitSound = script.Hit
local critSound = script.Crit
local shotSound = script.Shot
local reloadSound = script.Reloading
--GunSettings--
local fireRate = .1
local dmg = 20
local critDmg = 30
local magSize = script.magSize
local ammoCount = script.AmmoCount
local reloadSpeed = 2
--Shooting--
tool.Equipped:Connect(function(mouse)
print(player.Name.." equipped the Magnum!")
mouse.Button1Down:Connect(function()
if canShoot and ammoCount.Value > 0 and (reloading ~= true) then
shotSound:Play()
light.Enabled = true
ammoCount.Value = ammoCount.Value - 1
canShoot = false
local ray = Ray.new(firePart.CFrame.p, HRP.CFrame.lookVector * 300)
local part, position = workspace:FindPartOnRay(ray, character, false, true)
local beam = Instance.new("Part", workspace)
beam.BrickColor = BrickColor.new("New Yeller")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.25
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
local distance = (firePart.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.1, 0.1, distance)
beam.CFrame = CFrame.new(firePart.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
game:GetService("Debris"):AddItem(beam, 0.1)
wait(fireRate)
canShoot = true
light.Enabled = false
if part then
local playerH = game.Players.LocalPlayer.Character.Humanoid
local hit = part.Parent:FindFirstChild("Humanoid")
if not hit then
hit = part.Parent.Parent:FindFirstChild("Humanoid")
end
if hit then
shotEvent:FireServer(hit)
end
end
end
end)
--reload--
UserInputService.InputBegan:connect(function(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.R and ammoCount.Value < magSize.Value and (reloading ~= true) then
reloading = true
reloadNoti.Text = "RELOADING..."
reloadSound:Play()
wait(reloadSpeed)
ammoCount.Value = magSize.Value
wait()
reloadNoti.Text = ""
reloading = false
end
end)
end)
Server:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local hitSound = script.Hit
local critSound = script.Crit
local shotSound = script.Shot
local reloadSound = script.Reloading
local shotEvent = ReplicatedStorage.Events:WaitForChild("Shot")
local function hasBeenShot(hit)
local rand = math.random(4)
if rand == 4 then
critSound:Play()
print("hit.Name")
print("critical")
hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 100
else
hitSound:Play()
print(hit.Name)
hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 100
end
end
shotEvent.OnServerEvent:Connect(hasBeenShot)
Working old local script:
--Variables---
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local firePart = script.Parent:WaitForChild("firePart")
local canShoot = true
local UserInputService = game:GetService("UserInputService")
local reloading = false
local reloadNoti = player.PlayerGui.gunGui.Frame.reload
local character = player.Character
local HRP = character.HumanoidRootPart
local light = script.Parent.firePart.SpotLight
--Sounds--
local hitSound = script.Hit
local critSound = script.Crit
local shotSound = script.Shot
local reloadSound = script.Reloading
--GunSettings--
local fireRate = .1
local dmg = 20
local critDmg = 30
local magSize = script.magSize
local ammoCount = script.AmmoCount
local reloadSpeed = 2
--Shooting--
tool.Equipped:Connect(function(mouse)
print(player.Name.." equipped the Magnum!")
mouse.Button1Down:Connect(function()
if canShoot and ammoCount.Value > 0 and (reloading ~= true) then
shotSound:Play()
light.Enabled = true
ammoCount.Value = ammoCount.Value - 1
canShoot = false
local ray = Ray.new(firePart.CFrame.p, HRP.CFrame.lookVector * 300)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
local beam = Instance.new("Part", workspace)
beam.BrickColor = BrickColor.new("New Yeller")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.25
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
local distance = (firePart.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.1, 0.1, distance)
beam.CFrame = CFrame.new(firePart.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
game:GetService("Debris"):AddItem(beam, 0.1)
wait(fireRate)
canShoot = true
light.Enabled = false
if part then
local humanoid = part.Parent:FindFirstChild("Humanoid")
if not humanoid then
humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
end
if humanoid then
local rand = math.random(4)
if rand == 4 then
humanoid:TakeDamage(critDmg)
critSound:Play()
print("crit")
else
humanoid:TakeDamage(dmg)
hitSound:Play()
end
end
end
end
end)
--reload--
UserInputService.InputBegan:connect(function(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.R and ammoCount.Value < magSize.Value and (reloading ~= true) then
reloading = true
reloadNoti.Text = "RELOADING..."
reloadSound:Play()
wait(reloadSpeed)
ammoCount.Value = magSize.Value
wait()
reloadNoti.Text = ""
reloading = false
end
end)
end)

7 comments
Try using hit.Parent.Humanoid:TakeDamage(100)
https://developer.roblox.com/en-us/api-reference/function/WorldRoot/FindPartOnRayWithIgnoreList
I get "13:14:48.312 - Humanoid is not a valid member of Players 13:14:48.314 - Stack Begin 13:14:48.315 - Script 'Players.wildmanx20.Backpack.Magnum.ServerMain', Line 18 - function hasBeenShot 13:14:48.316 - Stack End". As the ray is detecting the player even though i told it not too in the local script Line 43
responding to mixginggengerina10 ^^
@Brandon1881 How would I use that? I replaced FindPartOnRay with it but i didn't work. i'm guessing I did it wrong. I got " 13:24:19.917 - Unable to cast value to Objects 13:24:19.918 - Stack Begin 13:24:19.919 - Script 'Players.wildmanx20.Backpack.Magnum.ClientMain', Line 43 13:24:19.920 - Stack End"
Can you try and see what hit prints?
It prints my username