Problem with checking distance

Here’s the script :

local billboard = script.Parent
local distance = 10
local rock = billboard.Parent
local runService = game:GetService("RunService")

local function checkDistance()
	game.Players.PlayerAdded:Connect(function(player)
		local character = player.Character
		if character and character:FindFirstChild("Humanoid") then
			local playerPosition = character.PrimaryPart.Position
			local randomOffset = Vector3.new(
				math.random(-distance, distance),
				playerPosition.Y,
				math.random(-distance, distance)
			)
			local areaWithinRadius = rock.Position + randomOffset
			local distanceToArea = (playerPosition - areaWithinRadius).Magnitude

			if distanceToArea <= distance then
				billboard.Enabled = false
			else
				billboard.Enabled = true
			end
		end
	end)

runService.Heartbeat:Connect(function()
	for _, v in pairs(game.Players:GetPlayers()) do
		checkDistance()
	end
	end)
end

the script is used to check the distance between the player and the part called “rock”, the script just doesn’t work and doesn’t returns anything, even the errors
Can anyone help?

game.Players.PlayerAdded:Connect(function(player)

Don’t do that. That is an event, not a loop
Instead, use game.Players:GetPlayers()

i.e.:

local billboard = script.Parent
local distance = 10
local rock = billboard.Parent
local runService = game:GetService("RunService")

local function checkDistance()
	for _, player in ipairs(game.Players:GetPlayers()) do
		local character = player.Character
		if character and character:FindFirstChild("Humanoid") then
			local playerPosition = character.PrimaryPart.Position
			local randomOffset = Vector3.new(
				math.random(-distance, distance),
				playerPosition.Y,
				math.random(-distance, distance)
			)
			local areaWithinRadius = rock.Position + randomOffset
			local distanceToArea = (playerPosition - areaWithinRadius).Magnitude

			if distanceToArea <= distance then
				billboard.Enabled = false
			else
				billboard.Enabled = true
			end
		end
	end
end

runService.Heartbeat:Connect(function()
	for _, v in pairs(game.Players:GetPlayers()) do
		checkDistance()
	end
end)

You are somehow using PlayerAdded connection, which only works when a player joined the server. You can just pass the player onto the function:


local billboard = script.Parent
local distance = 10
local rock = billboard.Parent
local runService = game:GetService("RunService")

local function checkDistance(player)
		local character = player.Character
		if character and character:FindFirstChild("Humanoid") then
			local playerPosition = character.PrimaryPart.Position
			local randomOffset = Vector3.new(
				math.random(-distance, distance),
				playerPosition.Y,
				math.random(-distance, distance)
			)
			local areaWithinRadius = rock.Position + randomOffset
			local distanceToArea = (playerPosition - areaWithinRadius).Magnitude

			if distanceToArea <= distance then
				billboard.Enabled = false
			else
				billboard.Enabled = true
			end
		end
end

runService.Heartbeat:Connect(function()
	for _, v in pairs(game.Players:GetPlayers()) do
		checkDistance(v) -- send player to function
	end
	end)

You also included the Heartbeat in the function (incorrect end placing), which will not run until you call it outside the function (I fixed that in the code)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.