I want the player to take damage when they are near fire.
The closer they are the more damage they take.
But I have no idea how to start
local fireObject = game.Workspace.Fire --Replace with your fire object
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(chr)
local damageCountDown = 500 --One damage per 1 seconds = 1000 ms
local maxRange = 7 --Max Range
local maxDamage = 30 --Max Damage
local delta = DateTime.now() --Wait function
local connection
connection = game:GetService("RunService").Heartbeat:Connect(function()
if chr == nil then connection:Disconnect() end
if DateTime.now().UnixTimestampMillis-delta.UnixTimestampMillis>damageCountDown then --If the CountDown is finished
local distance = (chr.HumanoidRootPart.Position-fireObject.Position).magnitude --Get the distance
if distance<maxRange then
chr.Humanoid.Health -= maxRange-(distance)/maxRange*maxDamage --Lost health from the distance
end
delta = DateTime.now() --Refresh for the wait Function
end
end)
end)
end)
1 Like
I would suggest using something like a sphere part and listening to .Touched events.
You could loop through every player every second for every fire but that might result in lag once you have 20 players and about 300 fire parts in the game.
So when the player touches the sphere, you add them to an table and perform a function every second to deal the damage.
Why add them to a table/array? That’s to avoid repeatedly adding the same player multiple times since they are likely to fire the .Touched event multiple times.
Once the distance is larger than the sphere’s radius/size, you can stop tracking players, remove from the table and stop dealing damage.
1 Like