Firing a remote event for nearby players

Hey everyone,

I’ve hit a wall recently in trying to get a remote event (Explosion camera shake) to fire on nearby clients. The jist of my script is for a lightning strike to occur whenever an enemy is in a storm area which then causes nearby players screens to shake. I’ve set the script up to fire on all clients but have realized this would be annoying to players that are nowhere near the storm area. What would the best way to implement this feature into the script? I was thinking a table but I’m not sure how to best implement it in this situation; so any suggestions or guidance would be appreciated. I’ve also linked the script in question below.

Original and Working script

local DAMAGE = 25
local INTERVAL = 2

local touchingHumanoids = {}

script.Parent.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Monster")
	if humanoid then
		touchingHumanoids[humanoid] = true
	end
end)

script.Parent.TouchEnded:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Monster")
	if humanoid then
		touchingHumanoids[humanoid] = nil
	end
end)

while true do
	for humanoid in pairs(touchingHumanoids) do
		humanoid:TakeDamage(DAMAGE)
		script.Splat:Play()
		game.ReplicatedStorage.Explosion:FireAllClients()
		script.Parent.Electric.Enabled = true
		script.Parent.Electric2.Enabled = true
		script.Parent.PointLight.Enabled = true
	end
	task.wait(INTERVAL)
	script.Parent.Electric.Enabled = false
	script.Parent.Electric2.Enabled = false
	script.Parent.PointLight.Enabled = false
	end

You can calculate the distance between the humanoids and the part in question. Assuming the parent of the script is the part, something like this will suffice:

local ActivationDistance = 16

for _,Player in pairs(game.Players:GetPlayers()) do
	local Character = workspace:WaitForChild(Player.Name) -- Unless you move the characters in another folder
	if (script.Parent.Position - Character.PrimaryPart.Position).Magnitude < ActivationDistance then -- Check if the distance between the player and the part is small enough
		game.ReplicatedStorage.Explosion:FireClient(Player)
	end
end

If you don’t wanna consider the distance on the Y Axis, then you can simply save their positions in new variables and replace the y value with 0.

1 Like

This worked like a charm and was actually much simpler than what I had in mind, thanks for the quick reply and the help!

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