Hi! I want to make a system that makes a sphere whenever a player presses “E” that does damage to everyone except the creator.
I currently have the system in place (Code Below) but I cannot figure out a reliable way to make that sphere not do damage to the person who placed the sphere. Can anyone help?
local HitBox = script.Parent
local Caster = script.Parent.Caster.Value
local function hurtPlayer(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = humanoid.Health - 10
end
end
HitBox.Touched:Connect(hurtPlayer)
The Caster value is correctly setting it’s value.
local HitBox = script.Parent
local Caster = script.Parent.Caster.Value
local function hurtPlayer(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChild("Humanoid")
if partParent.Name ~= Caster then
if humanoid then
humanoid.Health = humanoid.Health - 10
end
end
end
HitBox.Touched:Connect(hurtPlayer)
This is some previous code I made to solve this issue, for some reason it doesn’t work and doesn’t hurt anyone.
What this variable is actually doing, is legit saving the Value of that Value Object when you start to simulate the Play button, so unless if you’re Parenting it until later use it’s always gonna return back nil no matter what cause there’s nothing being assigned to it unless if there’s a Instance it can store
Instead, try just referencing the Caster alone and not the Caster.Value so that you can check it for later use when checking for the name
And 1 more thing, if you’re handling all of this on the client it will not replicate to the server because every individual client is set up differently, so each one of them is going to see different outcomes
If you want to combat this, use a RemoteEvent instead so that you can call an action from the client when necessary, and fire it to the server (And vice-versa) when it’s needed
-- Client Side --
local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local HitBox = script.Parent
local Caster = HitBox.Caster
local function hurtPlayer(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChild("Humanoid")
if partParent.Name ~= Caster.Value then
if humanoid then
RemoteEvent:FireServer(humanoid)
end
end
end
HitBox.Touched:Connect(hurtPlayer)
-- Server Side --
local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
RemoteEvent.OnServerEvent:Connect(function(Plr, TargetHum)
TargetHum.Health -= 10
end)
It depends on where you exactly place the HitBox Objects, for the “Server” script you have there in the HitBox, you can just simply put it inside ServerScriptService alone cause the OnServerEvent will know when Events are being firing from the client so there’s no need to be Cloning it multiple times
I’m pretty sure LocalScripts work fine provided they’re parented (Or a descendant) to the Character, so I’d recommend adding a couple of print() statements to debug what does and what doesn’t exactly work