Warn alert with magnitude

my code is good ???

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local CreateFrameModule = require(ReplicatedStorage.CreateFrameModule)

local spawnLocation = workspace.SpawnLocation

local function onPlayerAdded(player: Player)
	
	local function onCharacterAdded(char: Model)
		local humanoidRootPart = char.HumanoidRootPart:: BasePart
		
		local warnFrame = player.PlayerGui.TopGui.WarnFrame
		local warnTextLabel = warnFrame.WarnText
		
		local warnText = player.Name.." Attention vous depassez bientôt les limites de la map" -- This text is french text because i'm french x)
		
		local debounce = true

		while task.wait(0.1) do
			local magnitude = (humanoidRootPart.Position - spawnLocation.Position).Magnitude
			
			if magnitude >= 50 then
				
				if debounce then
					
					warnTextLabel.Text = warnText
					CreateFrameModule.CreateFrame(warnFrame, 3)
					
					debounce = false
				end
				
			else
				debounce = true
			end
		end
	end
	
	player.CharacterAdded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)

Provide a picture of the output please.

No, not really. Here’s a better way of doing this:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local CreateFrameModule = require(ReplicatedStorage.CreateFrameModule)

local spawnLocation = workspace.SpawnLocation

local function onPlayerAdded(player: Player)

	local function onCharacterAdded(char: Model)
		local humanoidRootPart = char.HumanoidRootPart

		local warnFrame = player.PlayerGui.TopGui.WarnFrame
		local warnTextLabel = warnFrame.WarnText

		local warnText = player.Name.." Attention vous depassez bientôt les limites de la map" -- This text is french text because i'm french x)

		humanoidRootPart.Changed:Connect(function)
			local magnitude = (humanoidRootPart.Position - spawnLocation.Position).Magnitude
			if magnitude >= 50 then
				warnTextLabel.Text = warnText
				CreateFrameModule.CreateFrame(warnFrame, 3)
			end
		end)
	end

	player.CharacterAdded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)
1 Like

if i use heartbeat for wait physcial ? its good ?

No not really. It’s better to use a ChangedEvent like I did.