How do I give a tool a E to use ability script?

As the title says, how do I give a tool a E to use ability script?
I have already tried to do

local Tool = script.Parent
local Player = script.Parent.Parent.Parent
local Character = Player.Character
local Humanoid = Character.Humanoid
local HitScript = Tool.HitScript
local UIS = game:GetService("UserInputService")
-- Ball
local Ball = script.Parent.DefaultBall
local character = Tool.Parent
-- Animation
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://13962251515"
-- Settings
local debounce = false -- Cooldown
Animation.Parent = Humanoid.Animator
local UIS = game:GetService("UserInputService") -- We do this so we can get the players inputs
local debounce = false

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		Humanoid.Health += 15
	end
end)

Tool.Activated:Connect(function(activated)
	local HitAnimPlay = Humanoid:LoadAnimation(Animation)
	if not debounce then
		debounce = true
		HitAnimPlay:Play()
		HitScript.Enabled = true
		wait(2)
		HitScript.Enabled = false
		debounce = false
	end
end)

but it doesn’t seem to work.

4 Likes

Is this a script or local script? Also where is it located? Nvm seems like it’s under the Tool

2 Likes

It is a regular script inside of a tool.

1 Like

You can’t use UserInputService inside Scripts, it can only be done in LocalScripts

1 Like

Should I add a localscript inside of the tool to do that? Also, would it affect everyone else?

2 Likes

Yes

Wdym by that? Like will everyone be able to press E? Yes, they will be able to do so. However, consider firing an RE to the server to handle the abilities because exploiters can spoof client side stuff

2 Likes

I still havent learnt remote events… (i need help with it as i dont understand it)
I meant “Would it effect everyone else” as in if the player had 20 health, and they click E to heal back to 35, would everyone else see the player as 35 health or 20?

1 Like

Afaik, humanoid.Health isn’t replicated when set client side for obv reasons, so you would have to fire an RE across to the server to replicate the change to all clients

1 Like

I know you have to do game.ReplicatedStorage.RemoteEvent:FireServer(), but i’m not sure how to make it work, so when it activates it changes health.

1 Like

Just connect the RE to a function in the server

game.ReplicatedStorage.IncrementHealth.OnServerEvent:Connect(function(player)
    local humanoid = player.Character:FindFirstChild("Humanoid")
    if not humanoid then return end
    humanoid.Health += 35
end)
1 Like

Doesnt work, I did this:

local UIS = game:GetService("UserInputService")
local Player = script.Parent.Parent.Parent
local Character = Player.Character
local Humanoid = Character.Humanoid
debounce = false

game.ReplicatedStorage.IncrementHealth.OnServerEvent:Connect(function(player)
	local humanoid = player.Character:FindFirstChild("Humanoid")
	if not humanoid then return end
	humanoid.Health += 15
end)

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		debounce = true
		game.ReplicatedStorage.IncrementHealth:FireServer()
		wait(6)
		debounce = false
	end
end)
1 Like

Bro, is that a script or local script? If its a local script, the OnServerEvent must be in a server script. If it’s a server script, the UIS connection must be inside a local script

Do I put the OnServerEvent in ServerScriptService or somewhere else?

First you will need to make a local script inside of the tool that will fire a remote event that has a function bound to its activation.

Inside the local script should look like this

UIS.InputEnded:Connect(function(input) 
    if input.KeyCode == Enum.KeyCode.E then
    game.ReplicatedStorage.UseAbility:FireServer() -- UseAbility is a remote event
    end
end

Now inside the script that handles the ability on the server side (where ever you have it) should look like this.

game.ReplicatedStorage.UseAbility.OnServerEvent:Connect(function(player) 
player.Character.Humanoid.Health += 15
end
end
2 Likes

Script in ServerScriptService. Also, I did a minor Animation optimization

Local Script (inside Tool)

local Tool = script.Parent
local Player = script.Parent.Parent.Parent
local Character = Player.Character
local Humanoid = Character.Humanoid
local HitScript = Tool.HitScript
local UIS = game:GetService("UserInputService")
-- Ball
local Ball = script.Parent.DefaultBall
local character = Tool.Parent
-- Animation
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://13962251515"

local HitAnimPlay = Humanoid.Animator:LoadAnimation(Animation)

-- Settings
local debounce = false -- Cooldown
local UIS = game:GetService("UserInputService") -- We do this so we can get the players inputs
local debounce = false

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
        game.ReplicatedStorage.IncrementHealth:FireServer()
	end
end)

Tool.Activated:Connect(function(activated)
	if not debounce then
		debounce = true
		HitAnimPlay:Play()
		HitScript.Enabled = true
		wait(2)
		HitScript.Enabled = false
		debounce = false
	end
end)

Server Script (inside ServerScriptService):

game.ReplicatedStorage.IncrementHealth.OnServerEvent:Connect(function(player)
	local humanoid = player.Character:FindFirstChild("Humanoid")
	if not humanoid then return end
	humanoid.Health += 15
end)
2 Likes

No clue what the problem is, but no errors print and nothing happens.

Add some prints in the OnServerEvent connection and tell me if they print

image
Worked, but health doesnt increase.

1 Like

Do

humanoid.Health = humanoid.Health + 15

Idk, also if you are doing this at MaxHealth no change will occur since ruble automatically clamps the value

1 Like

I removed health by using game.Workspace.BlueClothesPerson.Health -= 30, in console, and then trying to press E