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

This reload script doesn't work and doesn't change any values, what is wrong?

I tweaked this gun script to add gun animations, and edited the reload, but the reload doesn't work, nor does it change any of the values, what is wrong here?

	--//Reload Event
			mouse.KeyDown:connect(function(key)
				key:lower()
				if key == "r" then
					if not isReloading then
						if maxAmmo.Value > 0 then
							isReloading = true
							local ammoneeded = (clips.Value - ammo.Value)
							wait(reloadTime.Value)
							ammo.Value = (ammo.Value + ammoneeded)
							maxAmmo.Value = (maxAmmo.Value - ammoneeded)
							isReloading = false
							end
						elseif ammoneeded > maxAmmo.Value then
							isReloading = true
							wait(reloadTime.Value)
							ammo.Value = (ammo.Value + maxAmmo.Value)
							maxAmmo.Value = 0
							isReloading = false
						end
					end
				end
			end
		end
	end)
end)

rest of the script

--//Variables
local plr = game.Players.LocalPlayer
local tool = script.Parent.Parent
local hole = tool.Hole
local handle = tool.Handle
local s = tool.Sights
local sb = tool.SightsBack
local sl = tool.Slide
local slb = tool.SlideBack
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
local reloadTime = config.ReloadTime
local isReloading = false

--//Events
tool.Equipped:connect(function(mouse)
	tool.Activated:connect(function()
		if not isReloading then
			if ammo.Value > 0 then
		if debounce then
			--//Doesn't allow spamming
			debounce = false
			--//Gunani
			s.Transparency = 1
			sl.Transparency = 1
			sb.Transparency = 0
			slb.Transparency = 0
			--//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)
			
			--//Bullet Tracing
			if allowTracing.Value == true then
				--//Make part
				local trace = Instance.new("Part", workspace)
				trace.Material = Enum.Material.Neon
				trace.BrickColor = BrickColor.new("White")
				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(.1)
			s.Transparency = 0
			sl.Transparency = 0
			sb.Transparency = 1
			slb.Transparency = 1
			wait(coolDown.Value - .1)
			debounce = true
		end
			else
			--//Out of Ammo
		end
	end
	--//Reload Event
			mouse.KeyDown:connect(function(key)
				key:lower()
				if key == "r" then
					if not isReloading then
						if maxAmmo.Value > 0 then
							isReloading = true
							local ammoneeded = (clips.Value - ammo.Value)
							wait(reloadTime.Value)
							ammo.Value = (ammo.Value + ammoneeded)
							maxAmmo.Value = (maxAmmo.Value - ammoneeded)
							isReloading = false
							end
						elseif ammoneeded > maxAmmo.Value then
							isReloading = true
							wait(reloadTime.Value)
							ammo.Value = (ammo.Value + maxAmmo.Value)
							maxAmmo.Value = 0
							isReloading = false
						end
					end
				end
			end
		end
	end)
end)

1 answer

You cannot use KeyDown on a mouse, because I cannot find the event on Object Browser. Instead, use UserInputService like so.

game:GetService('UserInputService').InputBegan:connect(function(key)
	if key.KeyCode == Enum.KeyCode.R then
		--insert the rest of reloading stuff here
	end
end)

4 comments

0

KeyDown is a member of the API, but is deprecated. UserInputService is the correct answer but saying you can't use KeyDown is false info.

0

Yeah, and since it is deprecated, you cannot use it. So technically it is correct.

0

But it still exists for compatibility. Saying you "can't" use it is false info. Change your wording slightly and i'm happy to +1 <3

0

No, otherwise I will not have a compatible number as my reputation points.

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