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

How can I make this script only work when I have the right tool equipped?

I want this script just to work when I have the "Combat" tool equipped. The script worked before I added line 13. So how can I make it work?

local UserInputService = game:GetService("UserInputService")

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()

local Animation = character:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("Animation"))

local Damage = 5
local Debounce = 0.3
local Keybind = Enum.UserInputType.MouseButton1
local CanPunch = true

player.Backpack:WaitForChild("Combat").Equipped:Connect(function()

UserInputService.InputBegan:Connect(function(input,busy)
	if input.UserInputType == Keybind and not busy then
		if CanPunch == true then
			CanPunch = false
			Animation:Play()
			Animation.Looped = false
			game.ReplicatedStorage.Remotes.Punch:FireServer(Damage)
			wait(Debounce)
			CanPunch = true
			end
		end
	end)
end)

1 answer

Hi again CuzImArmy!

After adding the line 13 you can remove the UserInputService function as it became useless and also the variables it needed.

Also, I see you are giving the damage parameter on a local script, you should never do that because the exploiter can execute the event and give like 99999 Damage. To fix this you can just call the function without the param, get the player character on the server and look for the equipped tool. Then create a module script in the server to handle tools data;

local module = {}

local module.tools = { -- creata a table to handle tools data
	["MyToolName"] = { --the tool name so you can find it back in the server script
		["Damage"] = 1, --Then you're lovely stats!
		["Debounce"] = 1 --Then you're lovely stats!
	},
	 --You can copy and paste this as much as required!
}

return module -- return the module {tools}

In you server script when you receive the event do

local toolModule = require(MODULE PATH)

game.ReplicatedStorage.Remotes.Punch.OnServerEvent:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait() -- this will give the plr character, if it's not finded then we will wait using the plr.CharacterAdded:Wait() and it will return the character when it has finded it

	if char then
		local tool = nil
		for i , v in pairs(char:GetChildren()) do-- get all children of the character and iterate though every single instance
			if v:IsA("Tool") then -- this means it's a tool
				tool = v
			end
		end

		-- then we can find our tool data inside our toolModule
		if tool == nil then return end --if the tool is null we return to prevent errors
		local data = toolModule[tool] -- gets the data
		local damage = data.Damage -- and there we go you can use these params!
	end
end)

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