How can I make the part not explode on me, but on another Humanoid?

I currently cannot get this script to work, and I do not understand why it doesn’t work, as I am fairly new in scripting. I currently have a part getting taken from ReplicatedStorage to be sent as a projectile, however, I want this projectile to explode upon contact with another person, but not me. My script is

local explodePart = script.Parent
local humanoid = workspace.Parent:FindFirstChildOfClass("Humanoid")
local Character = game.player.LocalPlayer;
function explode()
explodePart.Touched:Connect(function(Hit)
	if Hit.Parent ~= Character then
	local explosion = Instance.new("Explosion", game.Workspace)
	explosion.Position = explodePart.Position
	explodePart:Destroy()
end

if explodePart.Touched.Parent:FindFirstChildOfClass("Humanoid"):Connect(function()

	explode()
end)

Any help is worth taking in.

1 Like

This script won’t work at all. game.player should be game.Players and if this is on a server script, game.Players.LocalPlayer won’t work. That only works on LocalScripts

There is also a random function.

I would suggest to look through basic scripting tutorials before attempting to make something like this.

A service known as player doesn’t exist :thinking: game.Players.LocalPlayer is what you want, but even still that isn’t even the Character alone lol

The main issue here is how you’re cloning this part either on the client side, or the server because either way you have to reference an ObjectValue inside the Part to check who exactly cast the Projectile (That’s what I do, anyways)

local ExplodePart = script.Parent
local Caster = ExplodePart.Caster

local function Explode(Hit)
    local Plr = game.Players:GetPlayerFromCharacter(Hit.Parent)

    if Plr and Plr ~= Caster.Value then
        local Explosion = Instance.new("Explosion")
        Explosion.Position = ExplodePart.Position
        Explosion.Parent = workspace
        ExplodePart:Destroy()
    end
end

ExplodePart.Touched:Connect(Explode)
1 Like

Making these tools is my learning scale. I don’t learn off of the traditional pages, it just won’t stick. I use DevForums and my infrequent asking and videos to help guide me. Telling me that I am incapable won’t change the fact that it would help me immensely if I were taught how to create things like this.

Thank you for this. For future use, how would I define myself as the person using the tool when the script is presented within the object in storage?

Depending on how you have your tool set up, what you can do is reference a ObjectValue inside the Part, as that’s capable of holding 1 entire Object/Instance for a Value to easily reference, & when you start to clone the Projectile, set the ObjectValue.Value to be the Caster who first created the Projectile

It could work something along the lines of this, as Tool.Parent would be referred to as the Character, and we can call game.Players:GetPlayerFromCharacter() to obtain the Player object to properly set the ObjectValue:

local Tool = script.Parent

local function Cast()
    local Char = Tool.Parent
    local Plr = game.Players:GetPlayerFromCharacter(Plr)

    if Plr then
        local Projectile = game.ReplicatedStorage.ProjectileNameHere:Clone()
        Projectile.Caster.Value = Plr.Value

        -- Reference any other additional properties you want to change: Position/Size/BrickColor/etc

        Projectile.Parent = workspace
    end
end

Tool.Activated:Connect(Cast)
local Object = game.Workspace.Part
local NonTargetPlrName = "" -- Your Username

Object.Touched:Connect(function(Touched) 
	local Humanoid = Touched.Parent:FindFirstChildWhichIsA("Humanoid")

	if Humanoid and game.Players:GetPlayerFromCharacter(Humanoid.Parent) then 

		if not Humanoid.Parent.Name == NonTargetPlrName then 
			Instance.new("Explosion", Humanoid.Parent).Position = Humanoid.Parent.HumanoidRootPart.Position 
		end
		
	end
end)

so i can only understand in ur script is when a player’s humanoid touched the ‘explode part’ in your script var then it will explode and you said in da tittle but on the other humanoid or other player’s humanoid

Yes, as currently it keeps hitting me instead of hitting the individual. It wouldn’t even react when hitting them, only me.

oop making the statement opposite is not a good idea mb

maybe try again with the ‘else’ instead

,

local Object = game.Workspace.Part
local NonTargetPlrName = "" -- Your Username 

Object.Touched:Connect(function(Touched) 
	local Humanoid = Touched.Parent:FindFirstChildWhichIsA("Humanoid")

	if Humanoid and game.Players:GetPlayerFromCharacter(Humanoid.Parent) then 

		if Humanoid.Parent.Name == NonTargetPlrName then
			
			--Either left it over or add a function you want
			
			else
			Instance.new("Explosion", Humanoid.Parent.HumanoidRootPart).Position = Humanoid.Parent.HumanoidRootPart.Position 
		end

	end
end)