Why is my ray cast gun with remote events not working?
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:
02 | local tool = script.Parent |
03 | local player = game:GetService( "Players" ).LocalPlayer |
04 | local firePart = script.Parent:WaitForChild( "firePart" ) |
06 | local UserInputService = game:GetService( "UserInputService" ) |
07 | local reloading = false |
08 | local reloadNoti = player.PlayerGui.gunGui.Frame.reload |
09 | local character = player.Character |
10 | local HRP = character.HumanoidRootPart |
11 | local light = script.Parent.firePart.SpotLight |
12 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
13 | local shotEvent = ReplicatedStorage.Events:WaitForChild( "Shot" ) |
14 | local ignoreList = { character } |
17 | local hitSound = script.Hit |
18 | local critSound = script.Crit |
19 | local shotSound = script.Shot |
20 | local reloadSound = script.Reloading |
26 | local magSize = script.magSize |
27 | local ammoCount = script.AmmoCount |
31 | tool.Equipped:Connect( function (mouse) |
32 | print (player.Name.. " equipped the Magnum!" ) |
34 | mouse.Button 1 Down:Connect( function () |
35 | if canShoot and ammoCount.Value > 0 and (reloading ~ = true ) then |
38 | ammoCount.Value = ammoCount.Value - 1 |
41 | local ray = Ray.new(firePart.CFrame.p, HRP.CFrame.lookVector * 300 ) |
43 | local part, position = workspace:FindPartOnRay(ray, character, false , true ) |
45 | local beam = Instance.new( "Part" , workspace) |
46 | beam.BrickColor = BrickColor.new( "New Yeller" ) |
47 | beam.FormFactor = "Custom" |
48 | beam.Material = "Neon" |
49 | beam.Transparency = 0.25 |
52 | beam.CanCollide = false |
54 | local distance = (firePart.CFrame.p - position).magnitude |
55 | beam.Size = Vector 3. new( 0.1 , 0.1 , distance) |
56 | beam.CFrame = CFrame.new(firePart.CFrame.p, position) * CFrame.new( 0 , 0 , -distance / 2 ) |
59 | game:GetService( "Debris" ):AddItem(beam, 0.1 ) |
66 | local playerH = game.Players.LocalPlayer.Character.Humanoid |
67 | local hit = part.Parent:FindFirstChild( "Humanoid" ) |
70 | hit = part.Parent.Parent:FindFirstChild( "Humanoid" ) |
74 | shotEvent:FireServer(hit) |
81 | UserInputService.InputBegan:connect( function (inputObject, gameProcessedEvent) |
82 | if inputObject.KeyCode = = Enum.KeyCode.R and ammoCount.Value < magSize.Value and (reloading ~ = true ) then |
84 | reloadNoti.Text = "RELOADING..." |
88 | ammoCount.Value = magSize.Value |
Server:
01 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
02 | local hitSound = script.Hit |
03 | local critSound = script.Crit |
04 | local shotSound = script.Shot |
05 | local reloadSound = script.Reloading |
07 | local shotEvent = ReplicatedStorage.Events:WaitForChild( "Shot" ) |
09 | local function hasBeenShot(hit) |
10 | local rand = math.random( 4 ) |
15 | hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 100 |
19 | hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 100 |
23 | shotEvent.OnServerEvent:Connect(hasBeenShot) |
Working old local script:
002 | local tool = script.Parent |
003 | local player = game:GetService( "Players" ).LocalPlayer |
004 | local firePart = script.Parent:WaitForChild( "firePart" ) |
006 | local UserInputService = game:GetService( "UserInputService" ) |
007 | local reloading = false |
008 | local reloadNoti = player.PlayerGui.gunGui.Frame.reload |
009 | local character = player.Character |
010 | local HRP = character.HumanoidRootPart |
011 | local light = script.Parent.firePart.SpotLight |
014 | local hitSound = script.Hit |
015 | local critSound = script.Crit |
016 | local shotSound = script.Shot |
017 | local reloadSound = script.Reloading |
023 | local magSize = script.magSize |
024 | local ammoCount = script.AmmoCount |
028 | tool.Equipped:Connect( function (mouse) |
029 | print (player.Name.. " equipped the Magnum!" ) |
031 | mouse.Button 1 Down:Connect( function () |
032 | if canShoot and ammoCount.Value > 0 and (reloading ~ = true ) then |
035 | ammoCount.Value = ammoCount.Value - 1 |
038 | local ray = Ray.new(firePart.CFrame.p, HRP.CFrame.lookVector * 300 ) |
039 | local part, position = workspace:FindPartOnRay(ray, player.Character, false , true ) |
041 | local beam = Instance.new( "Part" , workspace) |
042 | beam.BrickColor = BrickColor.new( "New Yeller" ) |
043 | beam.FormFactor = "Custom" |
044 | beam.Material = "Neon" |
045 | beam.Transparency = 0.25 |
048 | beam.CanCollide = false |
050 | local distance = (firePart.CFrame.p - position).magnitude |
051 | beam.Size = Vector 3. new( 0.1 , 0.1 , distance) |
052 | beam.CFrame = CFrame.new(firePart.CFrame.p, position) * CFrame.new( 0 , 0 , -distance / 2 ) |
055 | game:GetService( "Debris" ):AddItem(beam, 0.1 ) |
060 | light.Enabled = false |
062 | local humanoid = part.Parent:FindFirstChild( "Humanoid" ) |
065 | humanoid = part.Parent.Parent:FindFirstChild( "Humanoid" ) |
069 | local rand = math.random( 4 ) |
071 | humanoid:TakeDamage(critDmg) |
076 | humanoid:TakeDamage(dmg) |
088 | UserInputService.InputBegan:connect( function (inputObject, gameProcessedEvent) |
089 | if inputObject.KeyCode = = Enum.KeyCode.R and ammoCount.Value < magSize.Value and (reloading ~ = true ) then |
091 | reloadNoti.Text = "RELOADING..." |
095 | ammoCount.Value = magSize.Value |