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

How do I fix my tool (Gun) so when equipped it actually shows in my hand?

So ive inserted a gun modeled from blender, put a handle part into the tool, and scripted with filtering enabled, yet when I try to equip the tool it simply does not show.

Note I also got a error:

Players.GoodkidMad.Backpack.m1911.Scripts.Main:25: attempt to index local 'mouse' (a nil value)

-- Variables

local tool = script.Parent.Parent
local hole = tool.barrel
local handle = tool.Handle
local debounce = true
local config = tool.Config
local ammo = config.Ammo
local maxammo = config.MaxAmmo
local dmg = config.Damage
local allowtrace = config.AllowTracing
local clips = config.Clips
local range = config.Range
local plr = game:GetService("Players").LocalPlayer
local cooldown = config.CoolDown

-- Events

tool.Equipped:Connect(function(mouse)
	tool.Activated:Connect(function(mouse)
		if debounce then
			-- Doesnt Allow Spam
			debounce = false
			-- Ray Get, & Set
			mouse.Button1Down:connect(function()
			local ray = Ray.new(hole.CFrame.p (mouse.hit.p - hole.CFrame.p) * range.Value)
			local hit, position = workspace:FindPartOnRay(ray, plr.Character, false, true)
			
			if allowtrace.Value == true then
				-- Make part
				local trace = Instance.new("Part", workspace)
				trace.Material = Enum.Material.Plastic
				trace.BrickColor = BrickColor.new("Really black")
				trace.CanCollide = false
				trace.Anchored = true
				trace.Locked = true
				trace.Transparency = 0.2
				
				-- Show Direction
				local distance = (hole.CFrame.p - position).magnitude
				trace.Size = Vector3.new(0.22, 0.1, 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
					-- Double Damage Headshots
					if hit.Name == "Head" then
						humanoid:TakeDamage(dmg.Value*2)
					else
						-- Regular Damage bodyshots
						humanoid:TakeDamage(dmg.Value)
					end
			end
			end
			wait(cooldown)
			debounce = false
			end)
			end
	end)
end)

Would greatly appreciate a response.

1 comment

0

Also what is the issue with mouse being a nil value? Ive tried to script another weapon I loaded in from blender (Ak-47) but it always ends up being an issue of the tool not being able to be equipped or a mouse nil error? Any way to get around this and any tips would be much appreciatied.

2 answers

✓ Accepted answer

First things first - you have your button1down event which will create a new event on top of all the other times you equipped it. Your activated function will be fine instead since it fires every time a player clicks with the tool open.

I think your script is erroring saying mouse is nil because there are two mouse variables and the mouse variable that the script is referring to is the activated's mouse variable. Activated doesn't have a mouse parameter I think. Either way, you can get the mouse using Player:GetMouse() since this is a local script.

About your gun not showing, try without any scripts at all. Maybe it's not welded properly (if it's not a mesh in your handle this might be the case)

20 comments

0

when a new variable is made, if one exists with the same name then the new one would replace the other var in the higher scope (just for the current scope only though).

0

the first time he named a var mouse, it was an actual mouse but now it's nil because, as you said Activated doesn't give mouse

0

my mistake. read that completely wrong lol

0

??????? yes, it happens

0

I removed the button1down function. When i declare the mouse as a variable (local mouse = game.Players:GetMouse()) I get the error that GetMouse isnt a valid member of Players. Also does anyone have an answer or similar issue for the tool not actually being in your hand on equip? The handle part is a regular part as the rest of the gun is made of meshes if that information helps.

0

sorry, I wasn't specific enough: game.Players.LocalPlayer:GetMouse()

0

You need to name the handle 'Handle' specifically. I can take a look at the actual model to see why it's not showing up if you'd like.

0

I didnt make a weld script :p, I just created a quick one and the gun shows up now, Im going to try with the mouse issue and get right back to you!

0

Ok so I just tried the code still gives me an error: Players.GoodkidMad.Backpack.m1911.Scripts.Main:25: attempt to index local 'mouse' (a nil value) --- I dont think Rblx understands (mouse) anymore since filtering enabled is mandatory Im wondering how im going to get the mouse at this point.

0

is mouse capitalized?

0

Now it isnt giving me a mouse issue its giving me a new error code: Trying to call method on object of type: Vector3 with incorrect arguments.

0

If you would like I can make it a model & you can take a look for yourself?

0

yeah please do

0

Let's also see the line that's erroing.

0

The model: https://www.roblox.com/library/3852157765/M1911 ///// As far as which line the error code brings me to? Line 25 where i define the arguments of the new Ray.

0

Youn need a comma between the two arguments on that line. Should look something like this: ///////////////////////local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - toolHandle.CFrame.p).unit * 300)

0

want to talk in site chat or on sh discord?

0

Yea whats your discord?

0

join sh discord, and i'll be there @iucyy

0

pls mark as answered

Turns out I needed to weld the guns parts, as far as the gun shooting (raycast) I had to change the debounce, to canShoot and change the true & false around. Also make sure when your using the (mouse) term you define!

local mouse = game.Players.LocalPlayer:GetMouse()

Full code look like this now:

-- Variables

local tool = script.Parent.Parent
local hole = tool.barrel
local handle = tool.Handle
local canShoot = true
local config = tool.Config
local ammo = config.Ammo
local maxammo = config.MaxAmmo
local dmg = config.Damage
local allowtrace = config.AllowTracing
local clips = config.Clips
local range = config.Range
local plr = game:GetService("Players").LocalPlayer
local cooldown = config.CoolDown
local mouse = game.Players.LocalPlayer:GetMouse()
-- Events

tool.Equipped:Connect(function(mouse)
	tool.Activated:Connect(function()
		if canShoot then
			-- Doesnt Allow Spam
			canShoot = false
			-- Ray Get, & Set
			local ray = Ray.new(hole.CFrame.p, (mouse.hit.p - hole.CFrame.p) * range.Value)
			local hit, position = workspace:FindPartOnRay(ray, plr.Character, false, true)
			
			if allowtrace.Value == true then
				-- Make part
				local trace = Instance.new("Part", workspace)
				trace.Material = Enum.Material.Plastic
				trace.BrickColor = BrickColor.new("Really black")
				trace.CanCollide = false
				trace.Anchored = true
				trace.Locked = true
				trace.Transparency = 0.2
				
				-- Show Direction
				local distance = (hole.CFrame.p - position).magnitude
				trace.Size = Vector3.new(0.22, 0.1, 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
					-- Double Damage Headshots
					if hit.Name == "Head" then
						print("Headshot")
						humanoid:TakeDamage(dmg.Value*2)
					else
						-- Regular Damage bodyshots
						print("Bodyshot")
						humanoid:TakeDamage(dmg.Value)
					end
			end
			end
			wait(cooldown)
			canShoot = true
			end
	end)
end)

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