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

Gun isn't working how it should? (i mean how guns usually work :P)

Hey i made a script to my gun:

-- Algumas variaveis --
	local pistola = script.Parent
	local hole = pistola:WaitForChild("Hole")
	local mouse = game.Players.LocalPlayer:GetMouse()
	 
	-- Lista de Valores --
	local ammo = 12
	local maxAmmo = 64
	local damage = 20
	local headshotDamage = 50
	local range = 100
	local coolDown = 0.5
	 
	-- impedir spamming --
	local spam = false
	 
	-- Mudar o rato --
	pistola.Equipped:Connect(function()
	    mouse.Icon = "rbxassetid://358948941"
	end)
	 
	pistola.Unequipped:Connect(function()
	    mouse.Icon = ""
	end)
	 
	-- Disparo --
	pistola.Activated:Connect(function()
	    if spam == false then
	         spam = true
	        pistola.Handle.Disparo:Play()
	       local bullet = Instance.new("Part")
	       bullet.Size = Vector3.new(range,0.25,0.25)
	       local ray = Ray.new(hole.CFrame.Position, (mouse.Hit.Position - hole.CFrame.Position).Unit*range)
	       local hit, position = workspace:FindPartOnRay(ray, game.Players.LocalPlayer.Character)
	       local distance = (pistola.Handle.CFrame.p - position).magnitude
	       bullet.CFrame = CFrame.new(pistola.Handle.CFrame.p, position) * CFrame.new(0,0, distance/2)
	       bullet.Transparency = 0.5
	       bullet.CanCollide = false
	       bullet.Anchored = true
	       bullet.Parent = game.Workspace
	       bullet.Orientation = hole.Orientation
	       bullet.Position = hole.Position
               game:GetService("Debris"):AddItem(bullet, 0.5)
	        spam = false
	    end
	end)

But it isn't working. Could you tell me what's wrong with it and how can i fix it?

Also here's a video of what my gun is doing.

Ignore the part of my script where i speak portuguese. (pistola = gun)

2 answers

try nesting the activated event in the tool Equipped Event...... Talking about this.... dont use the activated event..use the actual mouse.Button1Down Event....You can access the players mouse in a LocaScript by writing... game.Players.LocaPlayer:GetMouse() or by passing a parameter to the Tool equipped Event.... Take a look at the following codes.


local tool = nil --Your tool path goes here 
local handle = tool.Handle -- The handle of your gun

tool.Equipped:connect(function(mouse)

--I defined  a players mouse through equipped event--

mouse.Button1Down:connect(function()

	
	--- Codes for your gun :D

-- I hope it works for you

end

end)

2 comments

0

Thx il try that out.

0

But could you send the full fix pls? If you cant il just try to figure this out :P

Your bullet size line is incorrect replace it with this

bullet.Size = Vector3.new(0.25,0.25,distance)

and move it down so that you can use the distance varaible in your size line

1 comment

0

Ok il try that out

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