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

Why is the 'mouse' nil in this script?

Line 23 is where the error comes in saying 'mouse is nil'. Go test it.

--//Variables
local plr = game.Players.LocalPlayer
local tool = script.Parent.Parent
local hole = tool.Hole
local handle = tool.Handle
local debounce = true
local config = tool.Config
local range = config.Range
local dmg = config.Damage
local coolDown = config.CoolDown
local clips = config.Clips
local ammo = config.Ammo
local maxAmmo = config.MaxAmmo
local allowTracing = config.AllowTracing

--//Events
tool.Equipped:connect(function()
	tool.Activated:connect(function(mouse)
		if debounce then
			--//Doesn't allow spamming
			debounce = false
			--//Ray Get, Set
local ray = Ray.new(hole.CFrame.p (mouse.Hit.p - hole.CFrame.p*range.Value) --ERROR

local hit, position = workspace:FindPartOnRay(ray, plr.Character, false, true)
			
			if allowTracing.Value == true then
				--//Make "part
				local trace = Instance.new("Part", workspace)
				trace.Material = Enum.Material.Neon
				trace.BrickColor = BrickColor.new("Orange")
				trace.CanCollide = false
				trace.Anchored = true
				trace.Transparency = 0.5
				
				--//Show Direction
				local distance = (hole.CFrame.p - position).magnitude
				trace.Size = Vector3.new(0.2,0.2, distance)
				trace.CFrame = CFrame.new(hole.CFrame.p,position) * CFrame.new(0, 0, -distance/2)
				
				--//Remove Debris
				game:GetService("Debris"):AddItem(trace, 0.1)
			end
			
			--//Hit Detection
			if hit then
				local humanoid = hit.Parent:FindFirstChild("Humanoid")
				if humanoid then
					if hit.Name == "Head" then
						--//Double damage on headshots
						humanoid:TakeDamage(dmg.Value*2)
					else
						--//Normal damage on body shots
						humanoid.TakeDamage(dmg.Value)
					end
				end
			end
			
			wait(coolDown.Value)
			debounce = true
		end
	end)
end)

1 answer

✓ Accepted answer

It's been a long while since I've worked with tools (Just don't use them if you're confident in making your own tool system) but I do know you're doing things you don't have to do.

First of all, you're putting the Activated() event inside an Equipped() event when Activated() fires only if the player has clicked AND while the tool is already equipped.

Wiki page on the Activated event.

The other problem is that mouse isn't defined in the Activated() event. That argument isn't found in this event (See wiki again). What you can do is just define it using a local variable since you also define the LocalPlayer.

--//Variables
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse() --Getting the mouse from the player

local tool = script.Parent.Parent
local hole = tool.Hole
local handle = tool.Handle
local debounce = true
local config = tool.Config
local range = config.Range
local dmg = config.Damage
local coolDown = config.CoolDown
local clips = config.Clips
local ammo = config.Ammo
local maxAmmo = config.MaxAmmo
local allowTracing = config.AllowTracing

--//Events
tool.Activated:connect(function()
	if debounce then
		--//Doesn't allow spamming
		debounce = false
		--//Ray Get, Set
		local ray = Ray.new(hole.CFrame.p (mouse.Hit.p - hole.CFrame.p*range.Value)
		--This time it shouldn't error

		local hit, position = workspace:FindPartOnRay(ray, plr.Character, false, true)

		if allowTracing.Value == true then
			--//Make "part
			local trace = Instance.new("Part", workspace)
			trace.Material = Enum.Material.Neon
			trace.BrickColor = BrickColor.new("Orange")
			trace.CanCollide = false
			trace.Anchored = true
			trace.Transparency = 0.5

			--//Show Direction
			local distance = (hole.CFrame.p - position).magnitude
			trace.Size = Vector3.new(0.2,0.2, distance)
			trace.CFrame = CFrame.new(hole.CFrame.p,position) * CFrame.new(0, 0, -distance/2)

			--//Remove Debris
			game:GetService("Debris"):AddItem(trace, 0.1)
		
		end

		--//Hit Detection
		if hit then
			local humanoid = hit.Parent:FindFirstChild("Humanoid")
			if humanoid then
				if hit.Name == "Head" then
					--//Double damage on headshots
					humanoid:TakeDamage(dmg.Value*2)
				else
					--//Normal damage on body shots
					humanoid.TakeDamage(dmg.Value)
				end
			end
		end

		wait(coolDown.Value)
		debounce = true
	end
end)



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