Damage OverTime not working

when i did that the damage just went on forever :frowning:

code:

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

local damagePerSecond = 4
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

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)

– Gave code identical to above post on accident. –

that code is the exact same…

My apologies. I thought you still had your own code.

It appears I made my code assume that you always input STEP as 1. Try this instead:

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

local damagePerSecond = 4
function damageovertimefunc(Humanoid, Dmg, Time, STEP)
	local tPass = 1
    while(Humanoid.Health > 0 and tPass <= Time)do
		Humanoid:TakeDamage(Dmg)
		tPass = tPass + STEP
        wait(STEP)
    end
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)

so i’m starting to think the code works; but theres an issue
a script in starterpack checks every 3 seconds if the person is infected, if they are it will fire the damage over time.
there is a boolvalue inside of the character named “Infected”
i get this error
attempt to concatenate string with instance
Screen Shot 2020-07-12 at 10.16.19 PM

codes:

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

local damagePerSecond = 4
function damageovertimefunc(Humanoid, Dmg, Time, STEP)
	local tPass = 1
    while(Humanoid.Health > 0 and tPass <= Time)do
		Humanoid:TakeDamage(Dmg)
		tPass = tPass + STEP
        wait(STEP)
    end
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)
	print(Character.."Is getting damaged")
	local infectedbool = Character:WaitForChild("Infected")
	infectedbool.Value = false
end)
local rp = game.ReplicatedStorage
local remotes = rp.Remotes
local virusremote = remotes.Virus

--[[virusremote.OnClientEvent:Connect(function(enemyplr,enemychar)
	--print(enemyplr.."'s Character is "..enemychar)
	print("ok")
	
end)]]--


local Player = game.Players.LocalPlayer

repeat wait() until Player

local Character = Player.Character

repeat wait() until Character

local VirusBool = Character:WaitForChild("Infected")

while wait(3) do
	print("ok you aren't")
	if VirusBool.Value == true then
		local cc = game:GetService("Lighting").Virus
		cc.Enabled = true
		print("bro virus is tru")
		virusremote:FireServer(Player,Character)
		--wait(4)
		--cc.Enabled = false
		--VirusBool.Value = false
	end
end

virusremote.OnClientEvent:Connect(function(Player)
	print("recieved event")
	local newchar = Player.Character
	newchar.Infected = false
end)

also the virus remote does not recieve the onclientevent

while wait(3) do is most likely the problem.
Maybe you meant repeat wait(3) until? wait(3) is a function and while()do is like a IF function, where inside the () is a statement like true

And unlike the IF function, the WHILE will repeat the IF statement until the statement is false. Think of it as while (statement) is true, do (code)

so i should have

while VirusBool.Value == true do
    virusremote:FireServer(Player,Character)
end

? or should i have

repeat wait(3) until VirusBool.Value == true do

end

This piece of code seems like your best bet.

so fully its:


repeat wait(3) until VirusBool.Value == true do
	local cc = game:GetService("Lighting").Virus
	cc.Enabled = true
	print("bro virus is tru")
	virusremote:FireServer(Player,Character)
end

edit: also still dosent solve the problem to my error…

Wait a moment,

while VirusBool.Value == true do
   virusremote:FireServer(Player,Character)
end

is correct. My bad.

full codes:

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

--[[virusremote.OnClientEvent:Connect(function(enemyplr,enemychar)
	--print(enemyplr.."'s Character is "..enemychar)
	print("ok")
	
end)]]--


local Player = game.Players.LocalPlayer

repeat wait() until Player

local Character = Player.Character

repeat wait() until Character

local VirusBool = Character:WaitForChild("Infected")

--[[repeat wait(3) until
	print("ok you aren't")
	if VirusBool.Value == true then
		local cc = game:GetService("Lighting").Virus
		cc.Enabled = true
		print("bro virus is tru")
		virusremote:FireServer(Player,Character)
		--wait(4)
		--cc.Enabled = false
		--VirusBool.Value = false
end]]--

while VirusBool.Value == true do
	local cc = game:GetService("Lighting").Virus
	cc.Enabled = true
	virusremote:FireServer(Player,Character)
end

virusremote.OnClientEvent:Connect(function(Player)
	print("recieved event")
	local newchar = Player.Character
	newchar.Infected = false
end)

--[[repeat wait(3) until VirusBool.Value == true do
	local cc = game:GetService("Lighting").Virus
	cc.Enabled = true
	print("bro virus is tru")
	virusremote:FireServer(Player,Character)
end]]--
local rp = game.ReplicatedStorage
local remotes = rp.Remotes
local virusremote = remotes.Virus

local damagePerSecond = 4
function damageovertimefunc(Humanoid, Dmg, Time, STEP)
	local tPass = 1
    while(Humanoid.Health > 0 and tPass <= Time)do
		Humanoid:TakeDamage(Dmg)
		tPass = tPass + STEP
        wait(STEP)
    end
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)
	local infectedbool = Character:WaitForChild("Infected")
	infectedbool.Value = false
end)

no errors no nothing now; it dosent even do any damage either now… this has proved to be really frustrating

Yeah, this is honestly very weird. I’m not sure what I could be doing wrong either. Sorry for not being able to give a solution here, but best of luck to you!

that should not cause an error it will just use the value he set to time.

Have you done debug prints? See how far it gets into the functions with prints

1 Like

I don’t think you understood what @EmilyBendsSpace said. You simply aren’t able to use the variable time because it is a variable used by the ROBLOX Lua Engine, and can’t be overwritten.

yes you can roblox will just use the local one you set.

Hold on, this isn’t even relevant to the conversation, so I’ll hush about this. You ARE right, but what started this is the fact that the @ekuz0diaa most likely meant Time instead of time.

This might be part of the solution if using the original code by OP.

1 Like