Damage OverTime not working

the script wont do damage overtime… i get a error like this :

local rp = game.ReplicatedStorage
local remotes = rp.Remotes
local virusremote = remotes.Virus

function damageovertimefunc(Humanoid,Dmg,Time,STEP)
 --Step = 0.15
    local t = 0
    repeat wait(STEP)
        t = t + STEP
		local currentDamage = Dmg/(time/STEP)
        Humanoid:TakeDamage(currentDamage) -- ?
    until Humanoid == nil or Humanoid.Parent == nil or t >= Time
end

virusremote.OnServerEvent:Connect(function(Player)
	print(Player)
	local Character = Player.Character
	print(Character)
	local humanoid = Character:WaitForChild("Humanoid")
	local length = 4;
	local damagepertimestep = 4;
	local timestep = 0.5;
	--[[local function doDamageThings()
		print("gg gamer")
   		for _ = 1, length, timestep do
        	humanoid:TakeDamage(damagepertimestep);
       		wait(timestep);
		end
	end]]--
	damageovertimefunc(humanoid,4,4,0.5)
	--Character.Infected = false
end)

Think you would have to add a function code at the bottom of the script in order for DamageOvertTimeFuc to activate. For example.

function Hit(Plr)

script.Parent.Touched:Connect(Hit)

Just an Tip.

my code has nowhere in it a touched event? this is completely random

1 Like

I know that I’m saying you would need to have Connect before you have a function.

It was clearly an example.

I’m saying you would need to have Connect before you have a function.

Well, that simply isn’t true. Functions can be called without a RBXScriptSignal, like this:

function myFunction()
    print("myFunction was called!")
end
myFunction()

Connections should only be used when required.

well could you help me with my issue instead of correcting his? or tell me what that error is trying to say?..

You are trying to deal a certain amount of damage every second, right? I haven’t looked into the code super hard, sorry! I’ll take a look now also.

1 Like

Alright, here’s some code I threw together for you. This should be a normal script located anywhere in your Workspace or ServerScriptService :slight_smile:

local damagePerSecond = 15
game.Players.PlayerAdded:Connect(function (player)
    player.CharacterAdded:Connect(function (character)
        while(character.Humanoid.Health > 0)do
            character.Humanoid:TakeDamage(damagePerSecond)
            wait(1)
        end
    end)
end)

This should work just fine. Happy game developing!

Edit: Just tested and it works as expected, assuming the force field is not on the player.

uhm well, this damage overtime is used for a variety of moves in my game, and i want to make it do damage overtime after the onserverevent has been triggered for a time, damage, timestep, and humanoid that i can set (not really required but i like having customizability)… so this wouldnt really help

Sure thing. I’ll transform the function to be more customizable below after the edit is made.

“time” I think you got the variable wrong?

time is a built-in function, you can’t use that as a local variable name.

2 Likes

so i need to do Time?
(30303030)

Judging from your code, yes. You should keep in mind that variable names in coding are case sensitive.

hmm, it did not work… i dont know why?.. this time no errors or anything

also here is the original post i used:

but i do not know what _G is so i tried making it a normal function…

local damagePerSecond = 15
function damageovertimefunc(Humanoid, Dmg, Time, STEP)
	local tPass = 1
    while(Humanoid.Health > 0 and tPass <= Time)do
		Humanoid:TakeDamage(Dmg)
		tPass = tPass + 1
        wait(STEP)
    end
end
-- Function damages Humanoid with Dmg per STEP seconds until Time seconds have passed (or Humanoid's health is at 0)
game.Players.PlayerAdded:Connect(function (player)
    player.CharacterAdded:Connect(function (character)
        damageovertimefunc(character.Humanoid, 4, 4, 0.5
    end)
end)

Here you go. Happy programming!

1 Like

well once again it should call the damage over time when a remote event gets a .OnServerEvent… thank you for your contributing though; i don’t need you to do everything for me ik how to program but idk what the issue with my code was…

You can just use the function I gave and plug that into your specific connections that you wanted. Best of luck!

1 Like

Hince an example.

30charssssss

Sorry, what? And is that a signature?

1 Like