Having issues with making a part that damage you overtime

I wish to make a part that damage you overtime while standing on the part OR move. If the player decide to move off the part then he/she shall not receive anymore damage.

I have tried many different ways but could not find a way to get it to work. I though maybe with a while loop it may work? I have involved my code below if you wish to try it out and I’m sorry for having less knowledge/messy code as I just finished my intern and wanted to get back to coding on roblox.

Thanks anyhow for reading and take care!

local part = script.Parent
local debounce = false
part.Touched:Connect(function(hit)
	
	local player = game.Players:GetPlayerFromCharacter(hit.parent)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	print(player,humanoid)
	if player and humanoid ~= nil and debounce == false and humanoid.MoveDirection.Magnitude >= 0 then
		
		while hit.Parent == hit.Parent and debounce == false do
			debounce = true
			humanoid:TakeDamage(5);
			task.wait(0.5)	
			debounce = false
		end
		
	end

end)

You could try using TouchEnded, but I wouldn’t recommend it as it doesn’t behave the way we want it to sometimes. You can use the ZonePlus module instead.

2 Likes

You can try out a popular module named ZonePlus. It’s pretty simple and accurate and a lot of developers use it already. Here’s an example code for achieving this using the ZonePlus module:

local ServerStorage = game:GetService("ServerStorage")
local Zoneplus = require(ServerStorage:WaitForChild("ZonePlus")) -- We get the module
local PlayersInZone = {}

local Container = --part reference goes here
local CF = Container.CFrame
local Size = Container.Size
local Zone = Zoneplus.fromRegion(CF, Size) -- We have our zone
local DamageDelay = 3 -- players in zone get damaged every 3 seconds

task.spawn(function()
    while task.wait(DamageDelay) do
        for _, player in ipairs(PlayersInZone) do
            local Damage = --Damage to deal here, for random damage, use math.random().
            local Character = player.Character or player.CharacterAdded:Wait()
            local Humanoid = Character:WaitForChild("Humanoid")
            Humanoid:TakeDamage(Damage)
        end
    end
end)

Zone.playerEntered:Connect(function(player)
    table.insert(PlayersInZone, player)
end)

Zone.playerExited:Connect(function(player)
    if table.find(PlayersInZone, player) then table[player] = nil end
end)
2 Likes

Thank you and GooberOnTheForums for this solution. I suppose the only way to do this then is using TouchEnded or this module?

Pretty much, but you should use the ZonePlus module for better results.

I see, problem is however. I do not want to be heavly depending on other people modules with all due respect. But if there is really no other solution then I will use it ye.

Thanks again you both for assisting me and have a nice day!

1 Like

Changed your code a bit as instance cannot be removed (player is a instance and PlayersInZone is not). So for anyone that wish to read upon this topic in the future they can use the following code:

local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Zoneplus = require(ReplicatedStorage:WaitForChild("ZonePlus")) -- We get the module
local PlayersInZone = {}

local Container = workspace.MusicIngame.Areas.ColdDamage
local CF = Container.CFrame
local Size = Container.Size
local Zone = Zoneplus.fromRegion(CF, Size) -- We have our zone
local DamageDelay = 1 -- players in zone get damaged every 1 seconds

task.spawn(function()
	while task.wait(DamageDelay) do
		for _, player in ipairs(PlayersInZone) do
			local Damage = 5 --Damage to deal here, for random damage, use math.random().
			local Character = player.Character or player.CharacterAdded:Wait()
			local Humanoid = Character:WaitForChild("Humanoid")
			Humanoid:TakeDamage(Damage)
		end
	end
end)

Zone.playerEntered:Connect(function(player)
	table.insert(PlayersInZone, player)
end)

Zone.playerExited:Connect(function(player)
	if table.find(PlayersInZone, player) then 
		table.remove(PlayersInZone)
	end
end)

Again, big thank you to you BandQueenForever for providing this code aswell as solving this issue.

1 Like

Here’s a model I made a few months back for this exact issue. Slow damage (injury) ONLY when touching this! - Roblox

Looks pretty helpful and seem to be working fine. I have not fully put it to the test however with multiable people and would a while loop not take a player performace away?

It probably would have an effect. I put a 1/2 second wait in there to slow it down.
Also, I only have a couple of these in a showcase I’m making so I don’t expect more than 1 or 2 players at a time.

1 Like

That can be a issue however as I notice where I use it is a pretty tight place. And people useally seem to go in the zone I wanna use it for with around 2 - 4 players.

I do appreciate however thinking along the journey, it does mean alot. The script I had from Darkness (@BandQueenForever) was really helpful and edited to my liking. However it is still not very functional and can cause bugs overtime with the local PlayersInZone. Where he sometimes save a username twice.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.