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

projectile script button hold instead of spam?

i have a projectile script:

local sound = script.fire 
local tweenservice = game:GetService("TweenService") 
local plr = game.Players.LocalPlayer

local tear1 = game.ReplicatedStorage.Player1tear 
local UIS = game:GetService("UserInputService")

UIS.InputBegan:connect(function(input) 
	if input.KeyCode == Enum.KeyCode.E then 
		print("Working") 
		sound:Play() 
		local clonetear = tear1:Clone()

		-- Touched event
		clonetear.Touched:Connect(function(hit)
			local hitHumanoid = hit.Parent:FindFirstChildOfClass("Humanoid") -- Check for a humanoid
			if hitHumanoid then 
				-- You'll need to set up some remote events to deal real damage.
				hitHumanoid:TakeDamage(game.StarterPlayer.StarterPlayerScripts.TearScript.Damage.Value) -- Deal damage
				game.Debris:AddItem(0, clonetear) -- Remove projectile
			end
		end)

		clonetear.Parent = game.Workspace 
		clonetear.CFrame = plr.Character.HumanoidRootPart.CFrame + plr.Character.HumanoidRootPart.CFrame.lookVector * 5
		tweenservice:Create(clonetear,TweenInfo.new(1,0,0),{CFrame = plr.Character.HumanoidRootPart.CFrame + plr.Character.HumanoidRootPart.CFrame.lookVector * 40,Transparency = 1},Enum.EasingStyle.Quint,Enum.EasingDirection.Out):Play() 
		game.Debris:AddItem(2.5,clonetear)
	end 
end)

theres a sound under the script called fire, a value under the script called damage and the projectile in replicated storage called player1tear. right now to shoot rapidly you need to spam the e key. i need it so that you can hold down the e key and the projectiles will shoot with custom firerate value.

1 answer

You can use a function called ":IsKeyDown" of UserInputService. It basically checks if a certain key is pressed.

if you want to make your gun full auto you could do something like this:

-- This code may crash your studio if ran. This is just an introduction. Do not run this code.
game:GetService("RunService").Stepped:Connect(function() -- fires function every frame
if UserInputservice:IsKeyDown(Enum.Keycode.E) then -- checks if key is down
	-- do stuff that you want
end)

If you want a custom firerate to it then you could implement a debounce variable that basically checks if you are shooting or not.

Heres a neat script for full auto with firerate:

local debounce = false 
local firerate = 0.1 -- in seconds

game:GetService("RunService").Stepped:Connect(function() -- fires function every frame
if UserInputservice:IsKeyDown(Enum.Keycode.E) then -- checks if key is down
	if debounce == false then -- checks if debounce is false
		debounce = true -- setting debounce to true so we cant fire
		-- do stuff that you want
		wait(firerate) -- waiting our firerate time 
		debounce = false -- setting debounce to false so we can fire again
	end
end)

although i highly recommend putting the debounce and firerate on a server-sided script because hackers can change the firerate to something very low using scripts.

1 comment

0

the ")" in the last end is red lined,

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