Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

FE Ray Gun Not Firing Help?

local event = game.ReplicatedStorage:WaitForChild("Events").Gun
local reloading = false
event.Shoot.OnServerEvent:connect(function(plr,mouse,Ammo,Gun)
	local function reload()
		for i = 1, 40 do
			reloading = true
			Ammo.Value = Ammo.Value + 1
			wait(1)
			plr.PlayerGui.Ammo.AmmoT.Text = "Ammo: "..Ammo.Value
			if i == 40 then
				reloading = false
			end
		end
	end
	local function shoot()
	local ray = Ray.new(Gun.Handle.CFrame.p, (mouse.Hit.p - Gun.Handle.CFrame.p).unit * 20)
	local beam2,position = game.Workspace:FindPartOnRay(ray,plr.Character,false,true)
	local beam = Instance.new("Part")
	beam.Name = "Beam"
	beam.Parent = game.Workspace
	beam.Shape = "Block"
	beam.Material = "Neon"
	beam.TopSurface = "Smooth"
	beam.BottomSurface = "Smooth"
	beam.Anchored = true
	beam.CanCollide = false
	beam.Transparency = 0.25
	beam.BrickColor = BrickColor.new("New Yeller")
	local distance = (Gun.Handle.CFrame.p - position).magnitude
	beam.Size = Vector3.new(0.3, 0.3, distance)
	beam.CFrame = CFrame.new(Gun.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
	Ammo.Value = Ammo.Value - 1
	if beam2 then
		local h = beam2.Parent:FindFirstChild("Humanoid")
		if not h then
			h = beam2.Parent.Parent:FindFirstChild("Humanoid")
		end
		local name1 = beam2.Parent.Name
		local name2 = beam2.Parent.Parent.Name
		print(h)
		if h then
			if not game.Players:FindFirstChild(name1) or game.Players:FindFirstChild(name2) then
			h:TakeDamage(1)
			beam2.Transparency = beam2.Transparency + 0.2
			print("Took Health")
			end
		end 
	end
	mouse.Button1Up:connect(function()
		beam:Destroy()
		end)
	end
	if Ammo.Value <= 0 then
		reload()
	end
	if Ammo.Value > 0 then
		if reloading == false then shoot() else print("Reloading...")end
	end
end)

This is a script in ServerScriptService ^

local plr = game.Players.LocalPlayer
local handle = script.Parent:FindFirstChild("Handle")
local Gun = script.Parent
local Ammo = Gun:FindFirstChild("Ammo")
local mouse = plr:GetMouse()
script.Parent.Equipped:connect(function()
	local event = game.ReplicatedStorage:WaitForChild("Events").Gun
	event.AmmoOpen:FireServer(Gun,Ammo)
	print("Sent!")
	if script.Parent.Equipped == true then
	mouse.Button1Down:connect(function()
	local event = game.ReplicatedStorage.Events.Gun
	event.Shoot:FireServer(mouse,Ammo,Gun)
	end)
	end
end)

script.Parent.Unequipped:connect(function()
	local event = game.ReplicatedStorage:WaitForChild("Events").Gun
	event.AmmoClose:FireServer(Ammo)
	print("Sent!")
end)

This is the script in the gun ^ LocalScript Help?

1 answer

It's initially a bit hard to tell what's going on here, because of the wierd indentation scheme. If there's one reason you learn and follow a style guide, let these sorts of bugs be it.

local event = game.ReplicatedStorage:WaitForChild("Events").Gun
local reloading = false
event.Shoot.OnServerEvent:connect(function(plr,mouse,Ammo,Gun)
    local function reload()
        for i = 1, 40 do
            reloading = true
            Ammo.Value = Ammo.Value + 1
            wait(1)
            plr.PlayerGui.Ammo.AmmoT.Text = "Ammo: "..Ammo.Value
            if i == 40 then
                reloading = false
            end
        end
    end

    local function shoot()
    	local ray = Ray.new(Gun.Handle.CFrame.p, (mouse.Hit.p - Gun.Handle.CFrame.p).unit * 20)
    	local beam2,position = game.Workspace:FindPartOnRay(ray,plr.Character,false,true)

    	local beam = Instance.new("Part")
    	beam.Name = "Beam"
    	beam.Parent = game.Workspace
    	beam.Shape = "Block"
    	beam.Material = "Neon"
    	beam.TopSurface = "Smooth"
    	beam.BottomSurface = "Smooth"
    	beam.Anchored = true
    	beam.CanCollide = false
    	beam.Transparency = 0.25
    	beam.BrickColor = BrickColor.new("New Yeller")

    	local distance = (Gun.Handle.CFrame.p - position).magnitude
    	beam.Size = Vector3.new(0.3, 0.3, distance)
    	beam.CFrame = CFrame.new(Gun.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

    	Ammo.Value = Ammo.Value - 1

    	if beam2 then
        	local h = beam2.Parent:FindFirstChild("Humanoid")
        	if not h then
            		h = beam2.Parent.Parent:FindFirstChild("Humanoid")
        	end

        	local name1 = beam2.Parent.Name
        	local name2 = beam2.Parent.Parent.Name
        	print(h)

        	if h then
            		if not game.Players:FindFirstChild(name1) or game.Players:FindFirstChild(name2) then
            			h:TakeDamage(1)
            			beam2.Transparency = beam2.Transparency + 0.2
            			print("Took Health")
            		end
        	end 
    	end

    	mouse.Button1Up:connect(function()
        	beam:Destroy()
        end)
    end

    if Ammo.Value <= 0 then
        reload()
    end

    if Ammo.Value > 0 then
        if reloading == false then 
		shoot() 
	else 
		print("Reloading...")
	end
    end
end)

A couple things immediately strike me about this script.

First of all, passing the mouse object to a server script strikes me as odd. I don't even think that's allowed. It doesn't make any sense, considering how FilteringEnabled works. In either case, it's definitely better practice (and better for latency) to pass mouse.Hit as an argument instead of the mouse object itself, and set up your event in the client script.

Second of all, the script seems to be waiting for something in ReplicatedStorage.Events.

local event = game.ReplicatedStorage:WaitForChild("Events").Gun

The client script waits for the same thing. If it were missing, or improperly named, neither script would do anything at all (or print anything to suggest that they were stuck).

In order to give a more accurate diagnosis, I'd need to know if the script gave output (if any), what it accomplished (if anything), or at least something more specific than NOT FIRING HELP?

This discussion is preserved as it was. New answers, comments, and votes are closed.