I have a script that is launching a grenade, using the debris system but if I aim on the ground, the player gets killed (the other players and the local player) but I only want the other players to get hit by the explosion of the grenade.
Here is the local script in “StartPlayerScripts” :
local debris = game:GetService("Debris")
game.ReplicatedStorage.Fire.OnClientEvent:Connect(function(plr, mouseHit, tool)
local rocket = game.ReplicatedStorage.Rocket:Clone()
rocket.Parent = workspace
rocket.CFrame = CFrame.new(tool.RocketHole.Position, mouseHit)
local bv = Instance.new("BodyVelocity", rocket)
bv.Velocity = CFrame.new(rocket.Position, mouseHit).LookVector * 100
rocket.Touched:Connect(function(hit)
if hit:IsA("BasePart") then
if hit.Parent ~= tool then
rocket:Destroy()
game.ReplicatedStorage.Fire:FireServer(rocket.Position, mouseHit)
end
end
debris:AddItem(rocket, 3)
end)
end)
And this is the Server Script in “ServerScriptService” :
Check out the docs on the Explosion instance. You can disable damage from the explosion and use the event explosion.Hit to detect the items hit from the explosion (which you can then filter out to damage players):
You could add a conditional statement in the server script to check if the affected player is the local player. If it is, then you can skip the explosion logic.
if plr == game.Players.LocalPlayer then
return
end
You can pass the player’s identifier as a parameter when calling the server function and compare it to the userId of the local player on the server side.
--Local Script in "StartPlayerScripts"
local debris = game:GetService("Debris")
game.ReplicatedStorage.Fire.OnClientEvent:Connect(function(plr, mouseHit, tool)
local localPlayer = game.Players.LocalPlayer
local rocket = game.ReplicatedStorage.Rocket:Clone()
rocket.Parent = workspace
rocket.CFrame = CFrame.new(tool.RocketHole.Position, mouseHit)
local bv = Instance.new("BodyVelocity", rocket)
bv.Velocity = CFrame.new(rocket.Position, mouseHit).LookVector * 100
rocket.Touched:Connect(function(hit)
if hit:IsA("BasePart") then
if hit.Parent ~= tool then
rocket:Destroy()
game.ReplicatedStorage.Fire:FireServer(localPlayer.UserId, rocket.Position, mouseHit)
end
end
debris:AddItem(rocket, 3)
end)
end)
--Server Script in "ServerScriptService"
local debris = game:GetService("Debris")
game.ReplicatedStorage.Fire.OnServerEvent:Connect(function(playerId, rocketPos, mouseHit)
local player = game.Players:GetPlayerByUserId(playerId)
if player == game.Players.LocalPlayer then
return
end
local explosionPart = Instance.new("Part", workspace)
explosionPart.Anchored = true
explosionPart.CanCollide = false
explosionPart.Transparency = 1
explosionPart.Position = rocketPos
local expoSound = game.ReplicatedStorage.ExplosionSound:Clone()
expoSound.Parent = explosionPart
expoSound:Play()
local explosion = Instance.new("Explosion", workspace)
explosion.Position = explosionPart.Position
debris:AddItem(explosionPart, 3)
end)