I can't fix the script on the endurance auto train

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!So that when a player gets on a part, he gets endurance if he has enough endurance

  2. What is the issue? Include screenshots / videos if possible!Didn’t find any problem, output is clean

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?Didn’t find a solution

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)

I want that when a player stands on a part, he gets endurance and if he has enough endurance and when he leaves the part, he stops getting endurance. I don’t get endurance in this script. I don’t I know what the problem is

As strange as it sounds, it is still relevant. I have been unable to fix it for 2 hours. I have tried everything. Help me fix it.

This is still relevant and I don’t know where the problem is

I was able to fix the script, I want to receive endurance 1 time at a time, I get 5 times at a time and I want when the player leaves the zone, he stops receiving endurance

local Players = game:GetService("Players")

-- Table with zones and endurance requirements
local zones = {
{zonePart = game.Workspace.LavaZone.Lava, requiredEndurance = 100, multiplier = 5, radius = 50}, 
	{zonePart = game.Workspace.qualityZone.quality, requiredEndurance = 1000, multiplier = 10, radius = 50},
-- Add new zones here in the same format
}

local function onTouched(player, zoneData)
	local endurance = player:WaitForChild("Endurance")
	local eZoneMulti = player:WaitForChild("eZoneMulti")

	-- Check if the player can gain stamina
	local isReceivingEndurance = true
	while isReceivingEndurance do
		task.wait(1.5) -- Delay between increments

		-- Check if there is a character and a HumanoidRootPart
		local character = player.Character
		if character and character:FindFirstChild("HumanoidRootPart") then
			local LavaDistance = (character.HumanoidRootPart.Position - zoneData.zonePart.Position).magnitude

			-- A condition for obtaining endurance
			if LavaDistance <= zoneData.radius and endurance.Value >= 100 then 
				eZoneMulti.Value = 20 -- Set multiplier
				if endurance.Value >= zoneData.requiredEndurance then
					endurance.Value = endurance.Value + (zoneData.multiplier * eZoneMulti.Value)
					print("Added endurance to player: " .. player.Name .. " Current Endurance: " .. endurance.Value)
else
print("Not enough endurance for player: " .. player.Name )
break -- Stop if you don't have enough stamina
				end
			else
print("Player is out of radius or does not have enough endurance.")
break -- Stop adding if the player is out of radius or lacks stamina
			end
		else
			print("HumanoidRootPart not found for player: " .. player.Name)
			break -- If HumanoidRootPart is not found, stop the loop
		end
	end
end

local function onTouchEnded(player, touchConnection)
	touchConnection:Disconnect() -- Disconnecting the connection
	print("Player left the zone: " .. player.Name)
end

local function onPlayerAdded(player)
	player.CharacterAdded:Connect(function(character)
		for _, zoneData in pairs(zones) do
			local touchConnection

			zoneData.zonePart.Touched:Connect(function(hit)
				if hit.Parent == character then
					print("Player entered zone: " .. zoneData.zonePart.Name)

					-- Starting to add stamina
					if not touchConnection then
						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, touchConnection)
								touchConnection = nil -- Reset touchConnection
							end
						end)
					end
				end
			end)

			-- Disconnect the connection when the character leaves the world
			character.AncestryChanged:Connect(function(_, parent)
				if not parent then
					if touchConnection then
						onTouchEnded(player, touchConnection)
					end
				end
			end)
		end
	end)
end

-- Connecting to the player addition event
Players.PlayerAdded:Connect(onPlayerAdded)

robloxapp-20241015-1638578.wmv (5.7 MB)

in this script, I have all the problems that I can’t fix, I spent the whole day porting over it
I wanted to get it every 1.5 seconds, but it doesn’t work like that for me and it gives out every second, I do not know how to fix it.

Still relevant I haven’t fixed it

Check out zoneplus

I know this, I already have type 2 zone, but here is the problem

Accidentally clicked on the solution

Why dont you use the module in the script?
Also, can you specify, shortly, what the problem is?

I sent a video, download it and watch it, I’m not working wait I want it to be given every 1.5 seconds endurance But for me it’s given right away and I don’t know how to fix it

Hard to tell from the video but it looks like its giving the stat every 1.5 seconds but multiple times
You can either use threads to solve this or have only 1 loop that is always active and just use variables (or attributes) per player

for ex. when a player enters a zone, do Player:SetAttribute("zone", zone.Name)
Then in the loop give the stats based off of the zone (Player:GetAttribute("zone"))

Can you tell me where exactly I could add this? I have a problem with it. I get endurance every time. I just got confused with this script.

You would have to rescript some.