Roblox health issue

so, i made a script that decreases someones health by 10 but instead it kills the player…

ive tried rounding the number,
deleting the custom health script,
and etc,etc

it still kills the player…

here is the code :

plr.Character.Humanoid.Health = plr.Character.Humanoid.Health - 10

1 Like

doing in loop? or the max health is 10?you need to give us full version code.we cant help without that.

1 Like

the code that decreases health by 10 :

game.ReplicatedStorage.ColaDrank.OnServerEvent:Connect(function(plr,itemname)
	if plr.Character.Humanoid.Health <= 99 then
		plr.Character.Humanoid.Health = plr.Character.Humanoid.Health - 10
		plr.Character.Humanoid:UnequipTools()
		wait(1)
		plr.Backpack[itemname]:Destroy()
	end
end)

Custom health script :

-- Gradually regenerates the Humanoid's Health over time.
local REGEN_RATE = 1/100 -- Regenerate this fraction of MaxHealth per second.
local REGEN_STEP = 10 -- Wait this long between each regeneration step.

--------------------------------------------------------------------------------

local Character = script.Parent
local Humanoid = Character:WaitForChild'Humanoid'

--------------------------------------------------------------------------------

while true do
	while Humanoid.Health < Humanoid.MaxHealth do
		local dt = wait(REGEN_STEP)
		local dh = dt*REGEN_RATE*Humanoid.MaxHealth
		Humanoid.Health = math.min(Humanoid.Health + dh, Humanoid.MaxHealth)
	end
	Humanoid.HealthChanged:Wait()
end

this probably won’t do anything but try doing

plr.Character.Humanoid.Health -= 10

the only other thing I could think of is the event being run multiple times at once

1 Like

i think the event is firing fast.that kills player instant.have you added a DB? (prevents spams) if u didnt adding it to the local script that fires event is best choice.

1 Like

it doesnt work but,
the event could be running like 30 times at once…

that would make sense
use a print statement to see how many times the event is being run

1 Like

no, i didnt add a db…

btw i dont know what a db is.

db = debounce
this prevents an event from being spammed

1 Like

so its just a spam preventer you may think like that

1 Like

will it be ok if i send the script thats firing to you?

if ok :

local toolequipped = false
local UserInputService = game:GetService("UserInputService")
local Humanoid = game:GetService("Players").LocalPlayer.Character:WaitForChild("Humanoid")
local Animation = script.BloxyCola
local Track = Humanoid:LoadAnimation(Animation)
local usable = true

while true do
	if usable == true then
		if script.Parent.Equipped then
			toolequipped = true
		elseif script.Parent.Equipped == false then
			toolequipped = false
		end
		UserInputService.InputEnded:Connect(function(io)
			if io.UserInputType == Enum.UserInputType.MouseButton1 then
				if toolequipped then
					if Humanoid.Health <= 99 then
						usable = false
						Track:Play()
						wait(1)
						Track:Stop()
						game.ReplicatedStorage.ColaDrank:FireServer(script.Parent.Name)
					end
				end
			end
		end)
	end
	wait()
end

using events in a loop is not good choice.would you accept if i send u a updated code?

1 Like

yes ,of course…
yes ,of course…
yes ,of course…

just added a db, this should work

local toolequipped = false
local UserInputService = game:GetService("UserInputService")
local Humanoid = game:GetService("Players").LocalPlayer.Character:WaitForChild("Humanoid")
local usable = true
local debounce = false

while true do
	if usable == true then
		if script.Parent.Equipped then
			toolequipped = true
		elseif script.Parent.Equipped == false then
			toolequipped = false
		end
		
		UserInputService.InputEnded:Connect(function(io)
			if io.UserInputType == Enum.UserInputType.MouseButton1 then
				if toolequipped then
					if Humanoid.Health <= 99 then
						usable = false
						wait(1)
						if debounce == false then
							debounce = true
							game.ReplicatedStorage.ColaDrank:FireServer(script.Parent.Name)
							task.wait(1)
							debounce = false
						end
					end
				end
			end
		end)
	end
	wait()
end
1 Like

thank you! ill add the animations myself i think…

yeah, removed them so that i could edit your code real quick! my fault :sweat_smile:

1 Like

oh wow i didnt noticed u already fixed.ima made the code for nothing then :confused:

1 Like

you can still send it
maybe it’s a faster version of mine and he ends up using
just wanted to keep the original structure of his code

one more revision actually
remove the wait(1) after usable = false

thanks for solving my problem,

Fevu,riddleminecart!!!

1 Like