#Trying to find if the player that touches a hitbox is different from player who placed hitbox

1 messages · Page 1 of 1 (latest)

burnt wyvern
#

I am a begginer trying to script a knife that when used, kills the other player. So far the knife spawns a hitbox, and checks who touches the hitbox. It prints the userid of whoever touches the hitbox. When I try to compare the userid from the person who touches the hitbox, to the person who places the hitbox it doesn't work.

#
local Knife = script.Parent
local Character = script.Parent.Parent
local Player = game.Players:GetPlayerFromCharacter(Character)

Knife.Activated:Connect(function()
    local KnifeHitBox = game.ReplicatedStorage.KnifeHitBox:Clone()
    local KnifeHitboxPOS = Knife.Handle.CFrame
    
    KnifeHitBox.CFrame = KnifeHitboxPOS
    KnifeHitBox.Parent = game.Workspace
    
    --Check to see if the knife touches a player
    KnifeHitBox.Touched:Connect(function(hit) --checks to see if part touching hitbox is player
            if hit.Parent:FindFirstChild("Humanoid") then
                print("HitPlayer")
                local hitplayer = game.Players:GetPlayerFromCharacter(hit.Parent)
                print(hitplayer.UserId)
                
                if Player and hitplayer.UserId ~= Player.UserId then --Checks to see if different player
                    hit.Parent.Health = 0                    
                end
            end
    end)
end)
#

Here is the output for when the part is touched

old pelican
old pelican
#
hit.Parent.Health = 0  

instead of that

burnt wyvern
old pelican
#

you need to do

hit.Parent.Humanoid.Health = 0  
burnt wyvern
old pelican
burnt wyvern
#
local Knife = script.Parent
local Character = script.Parent.Parent
local Player = game.Players:GetPlayerFromCharacter(Character)

Knife.Activated:Connect(function()
    local KnifeHitBox = game.ReplicatedStorage.KnifeHitBox:Clone()
    local KnifeHitboxPOS = Knife.Handle.CFrame
    
    KnifeHitBox.CFrame = KnifeHitboxPOS
    KnifeHitBox.Parent = game.Workspace
    
    --Check to see if the knife touches a player
    KnifeHitBox.Touched:Connect(function(hit) --checks to see if part touching hitbox is player
            if hit.Parent:FindFirstChild("Humanoid") then
                print("HitPlayer")
                local hitplayer = game.Players:GetPlayerFromCharacter(hit.Parent)
                print(hitplayer.UserId)
                
                if Character ~= hit.Parent then --Checks to see if different player
                    hit.Parent.Humanoid.Health = 0                    
                end
            end
    end)
end)

Right now the player who activates the knife gets killed, and other player stays unaffected. I tried changing the ~= to == to do the opposite, and no one died

old pelican
#

i think ur getting the backpack

#

idk doesnt matter its not the character

old pelican
burnt wyvern
old pelican
# burnt wyvern Isnt that the character?

if u print it
local Knife = script.Parent
local Character = script.Parent.Parent
local Player = game.Players:GetPlayerFromCharacter(Character)

print(Character)

old pelican
burnt wyvern
old pelican
#

i have the knife but its not there for me

burnt wyvern
#

I think you have to hold it

#

for it to be in workspace

old pelican
burnt wyvern
#

I did the same thing lol

old pelican
#

local Character = script.Parent.Parent
runs

#

its not being held but its still runs

#

if u put it in the activated function

#

it should work

old pelican
# burnt wyvern ```Lua local Knife = script.Parent local Character = script.Parent.Parent local ...
local Knife = script.Parent

Knife.Activated:Connect(function()
    local Character = script.Parent.Parent
    local Player = game.Players:GetPlayerFromCharacter(Character)
    
    local KnifeHitBox = game.ReplicatedStorage.KnifeHitBox:Clone()
    local KnifeHitboxPOS = Knife.Handle.CFrame
    
    KnifeHitBox.CFrame = KnifeHitboxPOS
    KnifeHitBox.Parent = game.Workspace
    
    --Check to see if the knife touches a player
    KnifeHitBox.Touched:Connect(function(hit) --checks to see if part touching hitbox is player
            if hit.Parent:FindFirstChild("Humanoid") then
                
                if Character ~= hit.Parent then --Checks to see if different player
                    hit.Parent.Humanoid.Health = 0                    
                end
            end
    end)
    
    task.wait(0.1)
    KnifeHitBox:Destroy()
end)
burnt wyvern
#
local Knife = script.Parent

Knife.Activated:Connect(function()
    local KnifeHitBox = game.ReplicatedStorage.KnifeHitBox:Clone()
    local KnifeHitboxPOS = Knife.Handle.CFrame
    local Character = script.Parent.Parent.Parent
    local Player = game.Players:GetPlayerFromCharacter(Character)
    
    
    KnifeHitBox.CFrame = KnifeHitboxPOS
    KnifeHitBox.Parent = game.Workspace
    
    --Check to see if the knife touches a player
    KnifeHitBox.Touched:Connect(function(hit) --checks to see if part touching hitbox is player
            if hit.Parent:FindFirstChild("Humanoid") then
                print("HitPlayer")
                local hitplayer = game.Players:GetPlayerFromCharacter(hit.Parent)
                print(hitplayer.UserId)
                if hit.Parent ~= Character then --Checks to see if different player
                    hit.Parent.Humanoid.Health = 0                    
                end
            end
    end)
end)

This is the current code. It kills both players, however I think after the player that was holding the knife dies, it goes back to killing neither

naive fulcrumBOT
#

studio** You are now Level 7! **studio

burnt wyvern
#

Yeah when I put your script in it works. im gonna try and figure out what we did differently

burnt wyvern
# old pelican thats not happening to me

I changed

if hit.Parent ~= Character then

to

if Character ~= hit.Parent then

and it fixed it. I have no idea how that works tho.

isn't it the same thing? like saying
if 2 ~= 3 then
if 3 ~= 2 then

old pelican
#

should be the same