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)
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
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?
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
game.ReplicatedStorage.IncrementHealth.OnServerEvent:Connect(function(player)
local humanoid = player.Character:FindFirstChild("Humanoid")
if not humanoid then return end
humanoid.Health += 35
end)
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)
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
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
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)