local Players = game:GetService("Players");
local MaxMoralLoweringDistance = 30;
Players.PlayerAdded:Connect(function(player) -- Fires when a player joins the game
player.CharacterAdded:Connect(function(character) -- Whenever the player character respawns
local humanoid = character:WaitForChild("Humanoid");
humanoid.Died:Connect(function() -- When the player dies
CheckNearbyTeamMembers(player, character.HumanoidRootPart.Position, "BlueTeam"); -- Run the distance check
print(character.Name.." Died");
end)
end)
end)
function CheckNearbyTeamMembers(diedplayer, position, team) --
local players = Players:GetChildren();
for i ,v in pairs(Players:GetChildren()) do
if (v and v ~= diedplayer and v.Character ~= nil and v.Character:FindFirstChild("HumanoidRootPart")) then -- You can add the team check here
local dist = (v.Character.HumanoidRootPart.Position - position).Magnitude;
if (dist < MaxMoralLoweringDistance) then -- If the distance is lower than the max range
-- Rest of your logic here
end
end
end
end
Code should be placed in ServerScriptService.
I’m not sure how you game or team logic is set up. But this should be straight forward to understand.
When a player joins the game, you hook up the CharacterAdded event to them. Every time their character respawns it hooks up a Died event to the Humanoid associated to that spawned character.
You can simply run a distance check and pass in data of the died player (To not distance check against the dead player), position of the dead character and the team they’re on. The rest of the code is up to you to manage.
Are you using a listener to listen whenever the value changes? Ill look in roblox studio now, what is the script for the textbox and wheres the textbox located?