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 local 'mouse' (a nil value)?

so i am making a gun script while i test if it works i come across an error i don't get since i myself made a mouse variable and the function itself is ':connect(function(mouse)'

i do not have much time to reply but all help is appreciated

THE ERROR IS AT LINE 18

script:

ammo = script.Parent.Ammo
clipsize = script.Parent.ClipSize
insideclip = script.Parent.StoredAmmo
shotfrom = script.Parent.Main
debounce = true
dmg = 16
range = 1000
allowtracing = true
player = game.Players.LocalPlayer
mouse = player:getMouse()

script.Parent.Equipped:connect(function()
	local char = script.Parent.Parent
	script.Parent.Activated:connect(function(mouse)
		if debounce then
			debounce = false
			
			local ray = Ray.new(shotfrom.CFrame.p (mouse.hit.p - shotfrom.CFrame.p) * range.Value)
			local hit, position = workspace:FindPartOnRay(ray, char, false, true)
			
			if allowtracing.Value == true then
				local trace = Instance.new('Part', workspace)
				trace.Material = Enum.Material.Neon
				trace.Color = Color3.new(255,255,0)
				trace.CanCollide = false
				trace.Anchored = true
				trace.Transparency = 0.3
				
				local distance = (shotfrom.CFrame.p - position).magnitude
				trace.Size = Vector3.new(0.2, 0.2, distance)
				trace.CFrame = CFrame.new(shotfrom.CFrame.p, position) * CFrame.new(0,0,-distance/2)
				
				-----
				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
							humanoid:TakeDamage(dmg.Value * 2)
						else
							humanoid:TakeDamage(dmg.Value)
						
					end
				end
			end
			wait(0.01)
			debounce = true
		end
	end)
end)

1 comment

0

http://wiki.roblox.com/index.php?title=API:Class/Tool/Activated mouse isn't a parameter of Tool.Activated event listeners

2 answers

ammo = script.Parent.Ammo
clipsize = script.Parent.ClipSize
insideclip = script.Parent.StoredAmmo
shotfrom = script.Parent.Main
debounce = true
dmg = 16
range = 1000
allowtracing = true
player = game.Players.LocalPlayer
mouse = player:getMouse()

script.Parent.Equipped:connect(function()
    local char = script.Parent.Parent
    script.Parent.Activated:connect(function() --You already have the mouse  defined, no need to get it here, it isn't even possible anywho
        if debounce then
            debounce = false

            local ray = Ray.new(shotfrom.CFrame.p (mouse.hit.p - shotfrom.CFrame.p) * range.Value)
            local hit, position = workspace:FindPartOnRay(ray, char, false, true)

            if allowtracing.Value == true then
                local trace = Instance.new('Part', workspace)
                trace.Material = Enum.Material.Neon
                trace.Color = Color3.new(255,255,0)
                trace.CanCollide = false
                trace.Anchored = true
                trace.Transparency = 0.3

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

                -----
                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
                            humanoid:TakeDamage(dmg.Value * 2)
                        else
                            humanoid:TakeDamage(dmg.Value)

                    end
                end
            end
            wait(0.01)
            debounce = true
        end
    end)
end)


1 comment

0

now the error is 'No method name passed in __namecall for Vector3 14:16:28.977 - Stack Begin' line18 still

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