I was wondering the approach I should take to create a regional based attack, and by regional I mean by broadcasting the attack to the clients within the area or range. So if one players was getting attacked by a mob and it did its attack any player within range or the area can also see that attack the mob did.
Not 100% sure about the structure here was my attempt at first
Server → Mob → If in range put players inside a table {Players} → Fire Clients within the table.
What do you mean by broadcasting so other players can see the attack? Do you mean some kind of first person pov that nearby players watch too? How did your attempt work?
A Possible idea here could be creating a temporary, invisible BasePart right at the position of when the attack hits the surface (basically the point of impact), and then calculate the range of the player from that specific part.
I’ve got Assistant to draft a piece of code, here:
local function isPlayerWithinRange(player, part, range)
-- Check if player and part exist
if player and player.Character and part then
-- Calculate the distance
local distance = (player.Character.HumanoidRootPart.Position - part.Position).Magnitude
return distance <= range
else
return false -- Player or part does not exist
end
end
-- Example usage
local player = game.Players.LocalPlayer
local part = game.Workspace.Part
local range = 10 -- 10 studs
if isPlayerWithinRange(player, part, range) then
print("Player is within range of the part.")
else
print("Player is not within range of the part.")
end
You can use that script to then autonomously ping all the clients with a remoteevent, one by one.
You would send a remote event to everyone in the range but just use a simple position check instead of creating a part for no reason
local function isPlayerWithinRange(player: Player, position: Vector3, range: number) : boolean
local character = player.Character
if not character then return false end
if player and player.Character and part then
-- Calculate the distance
local distance = (character:GetPivot().Position - position).Magnitude
return distance <= range
else
return false -- Player or part does not exist
end
end
-- Example usage
for _, player in Players:GetPlayers() do
if isPlayerWithinRange(player, position, range) then
-- Send a remote event to the player here, for example
attackEvent:FireClient(player, position, attackType)
-- Then you would render the attack on client
else
print("Player is not within range")
end
end
Apparently OP tried this or similar. He’s talking about “seeing” the attack? I don’t know what it means though. He might be talking about a camera POV change for players within range, but even then a simple range checker + remote events would do this easily.
Hello, sorry the late responses. More the less I was just trying to figure out the better structure for firing a remove event to clients only within the range check and then firing the remote through the table.
The code below is untested and put together to only show the Mob function, but shows the structure of how I organized the player’s
local Players = game:GetService("Players")
local Mob = {}
Mob.__index = Mob
function Mob:GetRangeFromPlayer(): {Player}
local PlayerInRange = {}
for _, Player in Players:GetPlayers() do
local Character = Player.Character
if not Character then return end
local Magnitude = (Character:GetPivot().Position - self.Instance:GetPivot().Position).Magnitude
local MaxDis = Mob.Config.FollowDistance
if Magnitude < MaxDis then
table.insert(PlayerInRange, Player)
end
end
return PlayerInRange
end
-- Inside the attack system somewhere
local RadiusPlayers = Mob:GetRangeFromPlayer()
RemoteEvent:FireClient(RadiusPlayers)
Luau doesn’t currently have a way to fire specifically to a table of clients. You will have to loop through the players within range and fire individually. I don’t know what needs your scripts have, but you could do this either in your attack system or pass the remote you want to fire to players in range into the mob function itself.
Firing inside the function:
local Players = game:GetService("Players")
local Mob = {}
Mob.__index = Mob
function Mob:RadiusEvent(remote)
for _, Player in Players:GetPlayers() do
local Character = Player.Character
if not Character then continue end
local Magnitude = (Character:GetPivot().Position - self.Instance:GetPivot().Position).Magnitude
local MaxDis = Mob.Config.FollowDistance
if Magnitude < MaxDis then
remote:FireClient(Player)
end
end
end
-- Inside the attack system somewhere
Mob:RadiusEvent(your_remote_event_here)
Firing from attack system somewhere:
local Players = game:GetService("Players")
local Mob = {}
Mob.__index = Mob
function Mob:GetPlayersInRadius(): {Player}
local PlayersInRange = {}
for _, Player in Players:GetPlayers() do
local Character = Player.Character
if not Character then continue end
local Magnitude = (Character:GetPivot().Position - self.Instance:GetPivot().Position).Magnitude
local MaxDis = Mob.Config.FollowDistance
if Magnitude < MaxDis then
table.insert(PlayersInRange, Player)
end
end
return PlayersInRange
end
-- Inside the attack system somewhere
local RadiusPlayers = Mob:GetPlayersInRadius()
for _,player in pairs(RadiusPlayers) do
RemoteEvent:FireClient(player)
end