Automatic Gun Script Erroring

Hi! I’ve been trying to code an FE compatible AK-47, and it works other than the fact I get one error that pauses the script and presents me from finishing my mouseButton1Up function.

This is the error I get:
18:23:58.028 - ServerScriptService.AutomaticCreation:36: attempt to index field ‘Parent’ (a nil value)

Here is the majority of the code. The error is line 36.

Thank you!

1 Like

You need to check if
part.Parent exists before doing
local humanoid = part.ParentFindFirstChild(“Humanoid”)

It works, but my gun still doesnt detect button1up yet, hm.

Any part that could be hit by a raycast must have a parent since raycasts only scan for parts in Workspace. There will never be a case where a part returned by a raycast has no parent – it will always be at least Workspace.

In this case, something is likely going wrong with passing the part to the remote function. @CheekySquid can you print out part on line 5 and post back the results?

I fixed that error, thanks for the paragraph by the way, but it still is not detecting the mousebutton1up.

You will need to post your code for mousebutton1up for anyone to help you. You will get help most easily by uploading a repro file that has all relevant code you’re testing, but has anything unrelated to the AK47 in your place removed. You can attach rbxl or rbxlx files with this button:

1 Like

Doing that right now haha!

It’s because you declare firing as local in your Button1Up event. Change it from local firing = false to firing = false. Declaring a variable as local means it only exists within the scope it’s declared in, even if there is a variable with the same name in a higher scope. Because it’s local, you’re changing the value of only that variable and not the firing you declared earlier.

2 Likes

I have it working, but the first time when I use the gun, if I tap it fires for like 17 times, then works normally after.

--[[Variables]]-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

local tool = script.Parent – get tool
local player = game:GetService(“Players”).LocalPlayer – get player
local mouse = player:GetMouse() – getting the mouse
local sound = tool:WaitForChild(“GunFire”) – gunshot sound
local torso = “” – nothing
local reloading = false – checks if reloading or not
local contextActionService = game:GetService(“ContextActionService”)
local bodyType = nil – nil for now but will check whether player is R6 or R15
local difference = 0 – space between head and mouse
local bullets = tool:WaitForChild(“Bullets”)
local replicatedStorage = game:GetService(“ReplicatedStorage”)
local events = replicatedStorage:WaitForChild(“Events”)
local functions = replicatedStorage:WaitForChild(“Functions”)
local akEvents = events:WaitForChild(“AKEvents”)
local akFunctions = functions:WaitForChild(“AKFunctions”)
local gunGui = tool:WaitForChild(“AKGUI”)
local reloadTime = “3”
local down = false
– Remote Events –
local equipAnimation = akEvents:WaitForChild(“EquipAnimation”)
local headshot = akEvents:WaitForChild(“Headshot”)
local reloadTwo = akEvents:WaitForChild(“Reload”)
local shootEvent = akEvents:WaitForChild(“ShootEvent”)
local unequipAnimation = akEvents:WaitForChild(“UnequipAnimation”)
– Remote Functions –
local checkBodyType = akFunctions:WaitForChild(“CheckBodyType”)
local fetchBulletsLeft = akFunctions:WaitForChild(“FetchBulletsLeft”)
–[[Find Body Type]]-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function findBodyType()
bodyType = checkBodyType:InvokeServer(tool)
print(bodyType)
end
–[[Reload Function]]------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function reload()
if bullets.Value < 31 then
reloading = true
reloadTwo:FireServer(tool.Reload)

player.PlayerGui:WaitForChild("AKGUI").Bullets.Text = "RELOADING"
wait(reloadTime)
bullets.Value = 31
player.PlayerGui:WaitForChild("AKGUI").Bullets.Text = "AK-47 - " .. bullets.Value.. "/31"
mouse.Icon = game.ReplicatedStorage.MouseValueDeagle.Value
equipAnimation:FireServer(tool.Shoot)
reloading = false
end

end
–[[Main Firing Script]]---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

tool.Equipped:Connect(function(theMouse)

gunGui:Clone().Parent = player.PlayerGui
findBodyType()
equipAnimation:FireServer(tool.Shoot)
local img = game.StarterGui.ImageLoader.DeagleHair.Image
mouse.Icon = img
mouse.Button1Down:Connect(function()

down = true
while down == true do
wait(.1)

		print("working")
	if bullets.Value <= 0 or reloading == true then
		--Don't do anything
	else
		
		local head = game.Workspace[player.Name].Head.CFrame.lookVector
		local theMouse = CFrame.new(game.Workspace[player.Name].Head.Position,theMouse.Hit.p).lookVector
		difference = (head - theMouse)
		local ray = Ray.new(tool.Handle.CFrame.p, (player:GetMouse().Hit.p - tool.Handle.CFrame.p).unit*250)
		local part,position = game.Workspace:FindPartOnRay(ray,player.Character,false, true)
		
		if difference.magnitude < 1.33 then
			sound:Play()
			bullets.Value = bullets.Value - 1
			shootEvent:FireServer(tool, position, part)

mouse.Button1Up:connect(function()
down = false
end)
end
end
end
end)

local reloadMobileButton = contextActionService:BindAction("ReloadBtn",reload,true,"r")
contextActionService:SetPosition("ReloadBtn",UDim2.new(0.72,-25,0.20,-25))
contextActionService:SetImage("ReloadBtn","http://www.roblox.com/asset/?id=10952419")

end)
–[[Tool Unequipped Event]]--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
tool.Unequipped:Connect(function()
mouse.Icon = “”
unequipAnimation:FireServer(tool.Shoot)
player.PlayerGui.AKGUI:Destroy()
contextActionService:UnbindAction(“ReloadBtn”)
end)

1 Like

A repro place would be nice so I could help out given I’m more of a hands-on person. As far as I can go without either, that error means you’re trying to perform an action on a nil parent.


By the way, Discourse has markdown, a lovely text formatting feature. You can use them to format your code when you post. For example, when you post code, do ```lua, start a new line, post your code, make one more new line, then close your block with three more ```.

```lua
print(“Hello”)
```

=

print("Hello")

This will catch all your code and not leave anything out, so that viewing it is easy and clear. :slight_smile:

2 Likes