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

"Attempt to index nil with ''changed' " error happening in my gun script?

I'm trying to make a game where you have guns, And im currently making a gun that functions as an AK47. So i want it to rapid fire until you take your finger off the mouse button. So im using values. However the 'Changed' Is somehow nil? Can somebody help?

local Shoot = script:FindFirstChild("Shoot")

script.Parent.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		Shoot.Value = true
	end)
end)

script.Parent.Equipped:Connect(function(mouse)
	mouse.Button1Up:Connect(function()
		Shoot.Value = false
	end)
end)

script.Parent.Equipped:Connect(function(mouse)
	Shoot.Changed:Connect(function()
		while Shoot.Value == true do
			game:GetService("ReplicatedStorage"):FindFirstChild("BRRRRREvent"):FireServer(script.Parent.ScopePart.Position, mouse.Hit.Position)
			wait(0.05)
		end
	end)
end)

Errors happening at


script.Parent.Equipped:Connect(function(mouse)
	Shoot.Changed:Connect(function()
		while Shoot.Value == true do
end)

1 comment

0

Value is also nil

1 answer

Okay, there's a hundred problems in this script.

local Shoot = script:FindFirstChild("Shoot") -- you don't need to make a physical bool value to check if the player is holding down the mouse button. This can be checked locally.

script.Parent.Equipped:Connect(function(mouse) 
    mouse.Button1Down:Connect(function() 
        Shoot.Value = true
    end)
end)

script.Parent.Equipped:Connect(function(mouse) --this function fires when the tool is equipped, it isn't constantly firing as the tool is equipped. 
    mouse.Button1Up:Connect(function()
        Shoot.Value = false
    end)
end)

script.Parent.Equipped:Connect(function(mouse) -- okay, i think I get what you're trying to do with this. You're wanting to see if the tool is equipped. You can use a boolean for that and change its values when the tool is equipped/unequipped. That's the proper way to use (Un)Equipped:Connect.
    Shoot.Changed:Connect(function() -- this function only fires once when the shoot value has changed
        while Shoot.Value == true do -- doesn't work like that
            game:GetService("ReplicatedStorage"):FindFirstChild("BRRRRREvent"):FireServer(script.Parent.ScopePart.Position, mouse.Hit.Position) --what in the world..
            wait(0.05)
        end
    end)
end)

This is how I would've gone about this:

local UIS = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local Equipped = false 
local mouseDown = false
local RunService = game:GetService("RunService")

script.Parent.Equipped:Connect(function()
	Equipped = true
end)

script.Parent.Unequipped:Connect(function()
	Equipped = false
end)

UIS.InputBegan:Connect(function(input,processed)
	if processed then
		return
	end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		mousedown = true
	end
end)
UIS.InputEnded:Connect(function(input,processed)
	if processed then
		return
	end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		mousedown = false
	end
end)

RunService.Stepped:Connect(function() --stepped fires every frame, this is what you use when you are trying to get something that is constantly updating, for example the player's mouse position
	if mousedown then
		game:GetService("ReplicatedStorage"):FindFirstChild("BRRRRREvent"):FireServer(script.Parent.ScopePart.Position, mouse.Hit.Position)
	end
end)

Let me know if you have any problems.

1 comment

0

I rewrote it. It works fine. Maybe worse then what you wrote. Sure. But i need to have some pride xD

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