I am trying to send a remote event when a explosion is created near the player in a radius.
The localscript is in starterpack
Code so far
local player = game.Players.LocalPlayer
game.Workspace.ChildAdded:Connect(function(child)
if child.ClassName == "Explosion" then
script.RemoteEvent:FireServer()
end
end)
Why trusting the client to tell the server an explosion occured near the client?
The client could lie…
If server place the explosion at certain coordinates, you could read the distance between the explosion center and the player to determine if its near or not the player.
Or when the explosion spawns, you could read the parts inside a created Region, to determine if a player is inside the Region/ExplosionRange
Or dumb solution, you could create an invisible part sphere that spawns at the center of the explosion, and read the Touched event on that part, to find if a player was inside the desired range
Oh and if you are using the Explosion Instance from roblox, you could use the Hit event too Hit Event
local Char = game.Players.LocalPlayer.Character
for _,Exp in pairs(workspace:GetChildren()) do
if Exp:IsA("Explosion") then
while Exp ~= nil do
wait()
local Mag = (Char.PrimaryPart.Position - Exp.Position).Magnitude
if Mag < 10 then
print("Explosive Nearby")
end
end
end
end
Ill Edit the script, brb
(It appears to only work on One at a time, ill work on it later)
Honestly I dont think thats a good approach.
Firstly it still in client side, if the client deletes that script, system is over…
Iterating the whole workspace? In that case would be better use the OP approach, using an event of ChildAdded()
Starting a while loop each time an Explosion is found? and waiting on the loop until “Exp” is nil, making the script to wait on each iteration that found an explosion? I dont see why.
I suggest server side, not iterate the workspace and probably no loops are required only events
local player = game.Players.LocalPlayer
local Char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
game.Workspace.ChildAdded:Connect(function(child)
if child.ClassName == "Explosion" then
local Exp = child
local Mag = (Char.PrimaryPart.Position - Exp.Position).Magnitude
if Mag < 10 then
print("Explosive Nearby")
script.RemoteEvent:FireServer()
end
end
end)