How do i make a healing system that have remote event, no cooldown to call remote event but it take time to heal
For Example:
(Client)
local rps = game:GetService("ReplicatedStorage")
local myremote = rps.myremote
-- some input are here
myremote:FireServer()
(Server)
local rps = game:GetService("ReplicatedStorage")
local myremote = rps.myremote
myremote.OnServerEvent:Connect(function(player)
--logics here
player.Humanoid.Health = 100
-- Health system
Well, with a basic remote setup like this an exploiter can just trigger the remote event whenever they want to instantly heal. In order to protect it, you have to take into account how the player should be able to heal. From here you can perform various checks for the legality of the move based on player character context. Examples:
Example 1: Player uses a consumable (e.g. healing potion)
Keep track of how much of this item the player has on the server.
Only allow the player to heal if they have enough of this item, then subtract.
Example 2: Player uses an ability (such as a healing spell)
Very similar to above, but you may consider for a cooldown too.
Only allow the player to heal if their ability is charged and available.
Example 3: Player activates a prompt next to a healing object
Check if the player character is close enough to the object.
So use table right?, and check if player have medkit or etc to make server code only run if object is in the table and how do i add item if need remote event for a check such consumable item from a shop
Think of remotes as asking the server to do something, not telling it to do something.
This in theory means the server should do all the validation the client did, and possibly more if suitable.
Remember that remote data can be basically anything, even in a shape you dont expect it to be, because exploiters can poke at it with all sorts of data, you should also verify arguments are what you think they are.
The only argument that you can be sure of is the player argument, since the server grabs this one on its own.
local myRemote: RemoteEvent = "some path"
myRemote.OnServerEvent:Connect(function(player)
local medkit = player.Character:FindFirstChild("Medkit")
if medkit and medkit:IsA("Tool") then
-- do stuff
-- remove medkit
end
end)
--ServerScript in ServerScriptService
local rps = game:GetService("ReplicatedStorage")
local remote = rps:WaitForChild("myremote")
remote.OnServerEvent:Connect(function(player)
local char = player.Character
if not char then return end
local humanoid = char:FindFirstChildOfClass("Humanoid")
if not humanoid then return end
local total = 80
local rate = 1
local given = 0
while humanoid.Health > 0 and given < total do
task.wait(1)
local heal = math.min(rate, total - given)
humanoid.Health = humanoid.Health + heal
given += heal
end
end)
Ok this version is a bit better. Now they can get hit again and it still only gives a given amount.
It can stack and should work from all players the same way.
It will override default health healing script, regardles of what you do in your custom script.
You are also forgetting that the connection might be duplicated each time player respawns
And you’re the “bad practices” police? I don’t even think you actually know how to program. Programming is full of testing, mistakes, and refining. Unless you have everything perfectly documented and working, mistakes are inevitable. Don’t be shy.. jump right in.
You never contribute with a script; you just criticize the process of figuring it out. I may not be perfect, but I always get things working in the end. Scripts have to integrate with the actual Health system, and many issues can arise along the way. You just need a starting point to build from. I’d like to meet someone who can write flawless code perfectly on the first try.
Functionally, it works fine. Damage, death, and other health logic behave normally.
The only thing that is bugging me at this point is the stacking part. It’s a bit of a hassle to handle that here, and it may be better to limit the ability to have more than one. It’s just not quite there yet, and that really needs to be addressed. Also some redundant checks could be removed.
I get you’re trolling, and honestly I do feel a bit sorry for you. It shows you’re troubled deeply.
I’m still not seeing any attempt at a script. I truly don’t mind if you prove me wrong as long as it helps solve the post. I think we just have very different goals in being here on the forums.
why is the total inside the remote call? does that mean on ever remote call the total will be at the max again? Instead of getting depleted after it has been used to 80?
That was just a number to test with.. It should get a fresh 80 on entering the loop, while not affecting anything that may be already there. The issue is that nothing prevents multiple calls from stacking. Not getting any feedback from the OP, so .. It actually should work even getting stacked. I don’t know if anyone would want that however.. I always just think this is too odd and go with the fixed quick heal, for sanity sake. Not even saying this is the way to do it. Just stumbling along here looking for how this could be done on Roblox.
// Define the health changed event and handler delegate.
public delegate void HealthChangedHandler(object source, float oldHealth, float newHealth);
public event HealthChangedHandler OnHealthChanged;
// Show in inspector
[SerializeField]
float currentHealth;
// Allow other scripts a readonly property to access current health
public float CurrentHealth => currentHealth;
// test values
[SerializeField]
float testHealAmount = 5f;
[SerializeField]
float testDamageAmount = -5f;
public void ChangeHealth(float amount) {
float oldHealth = currentHealth;
currentHealth += amount;
currentHealth = Mathf.Clamp(currentHealth, 0, maxHealth);
// Fire off health change event.
OnHealthChanged?.Invoke(this, oldHealth, currentHealth);
}
// Test code
void Update() {
if (Input.GetKeyDown(KeyCode.Q)) {
ChangeHealth(testHealAmount);
}
if (Input.GetKeyDown(KeyCode.E)) {
ChangeHealth(testDamageAmount);
}
}
--C#, specifically in the context of Unity