I created a script for automatic issuance of endurance, but the problem is that I get endurance several times every 1.5 seconds and I don’t know how to fix it, help
local Players = game:GetService("Players")
-- Table with zones and endurance requirements
local zones = {
{zonePart = game.Workspace.LavaZone.Lava, requiredEndurance = 100, multiplier = 5},
{zonePart = game.Workspace.qualityZone.quality, requiredEndurance = 1000, multiplier = 10},
-- Add new zones here in the same format
}
local function onTouched(player, zoneData)
local endurance = player:WaitForChild("Endurance")
local eZoneMulti = player:WaitForChild("eZoneMulti")
-- Start adding endurance
local isInZone = true
while isInZone do
wait(1) -- Delay between increments
if endurance.Value >= zoneData.requiredEndurance then
endurance.Value = endurance.Value + (zoneData.multiplier * eZoneMulti.Value)
else
break -- Stop if not enough endurance
end
end
end
local function onTouchEnded(player, zoneData, touchConnection)
touchConnection:Disconnect() -- Disconnect the connection
end
local function onPlayerAdded(player)
player.CharacterAdded:Connect(function(character)
for _, zoneData in pairs(zones) do
zoneData.zonePart.Touched:Connect(function(hit)
if hit.Parent == character then
-- Start adding endurance
local touchConnection
touchConnection = zoneData.zonePart.Touched:Connect(function(hit)
if hit.Parent == character then
onTouched(player, zoneData)
else
-- If the player leaves the zone, stop adding
onTouchEnded(player, zoneData, touchConnection)
end
end)
end
end)
end
end)
end
-- Connect to the player added event
Players.PlayerAdded:Connect(onPlayerAdded)```
Your player touches the Part & fires your onTouched function. Then when they touch it again (humanoids will do this) the function fires again, even if it just fired.
You need a debounce with a check to see if it’s been fired to keep it from happening over and over.
As @Scottifly mentioned the event is likely firing if the player touched the part multiple times which could be caused by walking on/in it or by multiple parts of the character touching it and triggering the function.
To overcome this, you could try adding a Tag to the character model using the :AddTag() function and adding an if which uses the :HasTag() function so that it won’t begin to run the function again if the character has the tag and is therefore already receiving endurance.
Similarly, to remove the tag, you would use the :RemoveTag() function.
It looks like you’re connecting multiple touched events, which could be the problem.
You can fix it by managing when a player leaves or renters the zone.
local Players = game:GetService("Players")
-- Table with zones and endurance requirements
local zones = {
{zonePart = game.Workspace.LavaZone.Lava, requiredEndurance = 100, multiplier = 5},
{zonePart = game.Workspace.qualityZone.quality, requiredEndurance = 1000, multiplier = 10},
-- Add new zones here in the same format
}
-- This function will handle adding endurance while the player stays in the zone
local function addEndurance(player, zoneData)
local endurance = player:WaitForChild("Endurance")
local eZoneMulti = player:WaitForChild("eZoneMulti")
-- Add endurance periodically
while player.isInZone and endurance.Value >= zoneData.requiredEndurance do
endurance.Value = endurance.Value + (zoneData.multiplier * eZoneMulti.Value)
wait(1) -- Wait 1 second between increments
end
end
local function onZoneTouched(player, character, zoneData)
if not player.isInZone then -- Make sure we're not already in the zone
player.isInZone = true
addEndurance(player, zoneData) -- Start adding endurance
end
end
local function onZoneTouchEnded(player, character)
player.isInZone = false -- Stop adding endurance when leaving the zone
end
local function onPlayerAdded(player)
player.isInZone = false -- Add an 'isInZone' flag to track when a player is in a zone
player.CharacterAdded:Connect(function(character)
for _, zoneData in pairs(zones) do
zoneData.zonePart.Touched:Connect(function(hit)
if hit.Parent == character then
onZoneTouched(player, character, zoneData)
end
end)
zoneData.zonePart.TouchEnded:Connect(function(hit)
if hit.Parent == character then
onZoneTouchEnded(player, character)
end
end)
end
end)
end
-- Connect to the player added event
Players.PlayerAdded:Connect(onPlayerAdded)
I added TouchEnded to stop the endurance increment