I want to create lava part, so whoever touch it will get damage constantly until they die, the problem is if player touch it, it only damage once, it will do it again if the players move inside the touched part. How do I damage players eventhough they are standing still without moving?
this is my code
fireOn.Touched:Connect(function(part)
local eHum = part.Parent:FindFirstChild("Humanoid")
if eHum.Parent.Name == player.Name then
eHum:TakeDamage(0.1)
end
end)
You can use BasePart.TouchEnded, if the player touches, add them to a table and remove them when the touchevent fires, then damage every player in the table how often you want
the problem is Touch Event only Fire if the player move inside touched part, the player can avoid damage by just standing still inside the part, I want to prevent that by damaging player inside that part constantly with/without the player moving.
yes… the touch event will be when they touch it at first, then you add them to the table, and the touchended event doesn’t fire until they stop touching it, which doesn’t happen until they move away
local plrtable = {}
local part = -- whatever part has the effect
part.Touched:Connect(function(hit) -- runs when player touches
if hit:FindFirstAncestorOfClass("Model"):FindFirstChildOfClass("Humanoid") then
local name = hit:FindFirstAncestorOfClass("Model").Name
plrtable[name] = true
end
end)
part.TouchEnded:Connect(function(hit) -- runs when player stops touching
if hit:FindFirstAncestorOfClass("Model"):FindFirstChildOfClass("Humanoid") then
local name = hit:FindFirstAncestorOfClass("Model").Name
plrtable[name] = false
end
end)
while wait(.1) do
for i, v in pairs(plrtable) do
if v then -- check if they are touching
game.Players[i].Character.Humanoid.Health -=5 -- change 5 to custom amount
end
end
end
Basically you want the events to sync for a player, because events are async threads they have no regard to one another.
Simply add to each player a boolean and set it to false, you can call it TouchingLava for example.
Set it from the server and let the server control it for security.
When a player touches the lava, if player.TouchingLava is false then
-set TouchingLava to true
-damage
-task.wait whatever time between damage you want (3 seconds for example)
-set TouchingLava back to false
So after 3 seconds, TouchingLava = false and i wont get damage, it is actually not I want, I want constantly get damage when touching the lava, but based on the GIF I sent above, the TouchEnded event fire even though I was still standing on the part, how do I check if I’m not on the part anymore?
Hmm… this means I will die even though I’m outside the Lava right? TouchingLava only set to false when my health is 0, that’s not what I want, what i want is player won’t get damage outside the Lava, it means I have to check if the player is not in the lava anymore, how?
You could create a very slim invisible part above the lava part, and since the touched event only runs whenever a part is moving in or out of another one, you could create a while loop and change the CFrame of the invisible part so that whenever the player is standing in the lava, they constantly get hit by this invisible part, thus taking damage.
Instead of detecting if the lava part is touched, you would detect if this slim invisible part is touched.
Remember the part has to be as slim as possible, so that its hit box is as small as possible width wise, however make the length the same size as the lava part obviously.
This is kind of a performant-heavy solution as the CFrame of the part will most likely be changing every 0.5 seconds or whatever, but it’s the best I can come up with right now since I don’t have access to my computer.
while task.wait(DAMAGE_TIME) do -- Time between damage taken. Change it to whatever you want.
local results = workspace:GetPartsInPart(fireOn)
local players = {}
for _, part in pairs(results) do
local humanoid = part.Parent:FindFirstChild("Humanoid")
if humanoid and not players[part.Parent] then
players[part.Parent] = true
humanoid:TakeDamage(0.1)
end
end
end