i am trying to make a script that heals the player when they press "C", but when i test it, it does heal but it quickly goes back to the original hp as if it didn't heal!
i tried testing the same script code into a part and made it heal players when touched, and then it works! this is so confusing!!!
here is the script that i did
local m = game.Players.LocalPlayer:GetMouse() local cooldown = false m.KeyDown:connect(function(k) k = k:lower() if k == "c" then if cooldown == false then cooldown = true local sparks = Instance.new("Sparkles") sparks.Parent = game.Players.LocalPlayer.Character.HumanoidRootPart sparks.SparkleColor = Color3.new(255, 255, 0) game.Players.LocalPlayer.Character.Humanoid.Health = game.Players.LocalPlayer.Character.Humanoid.Health + 10 wait(2) sparks:Destroy() cooldown = false end end end)
by the way i am still learning how to script so please don't judge if i did something dumb in this
Local scripts only affect the client, or your computer. It doesn't make any changes to the server itself, therefore no one else sees the change and the server says that your health never changes. Use a regular script instead.
Use a RemoteEvent
. That way, a local script and server script can communicate and share data with each other. Here's an example of using a RemoteEvent. The remote event is inside of the server script (server script = regular script) inside of ServerScriptService
.
--Server Script/Regular Script script.RemoteEvent.OnServerEvent:Connect(function(player) print(player.Name.." fired the server event!") end)
Local Script:
game:GetService("ServerScriptService").Script.RemoteEvent:FireServer()
Output:
Player1 fired the server event!
The KeyDown
method should not be used. Opt for UserInputService
or ContextActionService
instead! The reason we use these instead of KeyDown
is because KeyDown
can only check for keyboard presses. UserInputService
can detect touchscreen and Xbox controller inputs. ContextActionService
can detect multiple keys being pressed at once and other key combinations. Since we're only using one key, we're going to use UserInputService
. These services must be used in a Local Script, as the server is not allowed to detect what keys you are pressing.
local UIS = game:GetService("UserInputService") UIS.InputBegan:Connect(function(key) if key.UserInputType == Enum.UserInputType.Keyboard then print("You're pressing the "..key.KeyCode.." key.") end end)
Server Script/Regular Script:
script.RemoteEvent.OnServerEvent:Connect(function(player) print(player.Name.." clicked the C button.") end)
Local Script:
local UIS = game:GetService("UserInputService") local cooldown = false UIS.InputBegan:Connect(function(key) if key.UserInputType == Enum.UserInputType.Keyboard and not cooldown then if key.KeyCode == Enum.KeyCode.C then --When C is pressed... cooldown = true game:GetService("ServerScriptService").Script.RemoteEvent:FireServer() wait(2) cooldown = false end end end)
Server Script/Regular Script:
script.RemoteEvent.OnServerEvent:Connect(function(player) local humanoid = player.Character:FindFirstChild("Humanoid") if humanoid then --If humanoid exists then... local sparks = Instance.new("Sparkles") game:GetService("Debris"):AddItem(sparks, 2) sparks.SparkleColor = Color3.new(1,1) sparks.Parent = player.Character:FindFirstChild("HumanoidRootPart") humanoid.Health = humanoid.Health+10 end end)
Local Script:
local UIS = game:GetService("UserInputService") local cooldown = false UIS.InputBegan:Connect(function(key) if key.UserInputType == Enum.UserInputType.Keyboard and not cooldown then if key.KeyCode == Enum.KeyCode.C then --When C is pressed... cooldown = true game:GetService("ServerScriptService").Script.RemoteEvent:FireServer() wait(2) cooldown = false end end end)
Few things to note:
I use Debris
instead of Destroy
to remove the sparkles. AddItem
essentially does the same thing as Destroy
, except after a certain amount of time. I put 2 seconds in the second argument, the sparks will be destroyed in 2 seconds. I said Color3.new(1,1)
instead of 255,255, 0
. If you're using Color3.new
, every number will be multiplied up by 255, so it's actually equal. I left the last argument blank, because if you leave it blank then it will default to 0. Use Color3.fromRGB
instead of Color3.new
if you are using numbers 0-255 instead of 0-1.
Hope it helps!
Hello! So there are a couple of things wrong with this script. For one mouse.KeyDown is deprecated as theking48989987 has said. But also now that all games are "fliterenabled" you will need to alot more to have the health be the same value server side.
Place a "remoteEvent" inside of replicated storage and call it "Heal_Me". A "remoteEvent" is used to have localscripts comunicate to the server and vice versa. You can learn more on remoteEvents here and learn about remoteFunctions and remoteEvents here.
So once you placed the remoteEvent, make a server script and place it in serverScriptService. After that place a local script in game.StarterPlayer.StarterPlayerScripts, or StarterGui or StarterPack, but getting into the habbit of placing localscripts in game.StarterPlayer.StarterPlayerScripts is a great practice.
But as seeing this is a tool, i would keep it where it is.
Now this script in no way is the most secure, as it only does what you want, and can be edited by the player with exploits, or even worse, everyday glitches, so be careful, and edit upon this script if you want to make it more secure.
so heres the normal script that you placed inside serverScriptService:
--Wait for user to want to heal game.ReplicatedStorage.Heal_Me.OnServerEvent:Connect(function(o) o.Character.Humanoid.Health = o.Character.Humanoid.Health+10 print(o.Name.. " is healed by 10") end) --Surprising, not much server side, right?
After that, edit your script and use:
local cooldown= false local uis= game:GetService("UserInputService") uis.InputBegan:connect(function(key) --The key value normally has a .KeyCode attribute. Enum.KeyCode has all keys it can be. because you want C we do Enum.KeyCode.C if key.KeyCode== Enum.KeyCode.C then --Basicly what you had in your code with the healing being the remoteEvent. if cooldown==false then cooldown = true local sparks = Instance.new("Sparkles") sparks.Parent = game.Players.LocalPlayer.Character.HumanoidRootPart sparks.SparkleColor = Color3.new(255, 255, 0) game.ReplicatedStorage.Heal_Me:FireServer() wait(2) sparks:Destroy() cooldown = false end end end)
Now i didnt test this, so if it doesnt work i can always fix it. but it should work fine.