#Trying to find if the player that touches a hitbox is different from player who placed hitbox
1 messages · Page 1 of 1 (latest)
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
instead of checking the userId you could check if the hit.Parent ~= Character if its true its a different player
also
hit.Parent.Health = 0
instead of that
That makes sense I will try that. But why didn't my method of checking the userid work?
you need to do
hit.Parent.Humanoid.Health = 0
you needed to do .Humanoid
Before I changed it to killing the player, I had
print("Different Player")
and it wouldnt print
not sure
also ur never deleting the knife hitbox when you clone
Yeah I know, I am working on the script bit by bit lol\
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
ur not getting the character
local Character = script.Parent.Parent
i think ur getting the backpack
idk doesnt matter its not the character
which is why the userid didnt work cause you couldnt get the player if you didnt have the character
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)
wait how are you giving the player a knife
Currently under Starterpack
i have the knife but its not there for me
oh im stupid
I did the same thing lol
anyways the problem is when
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
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)
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
** You are now Level 7! **
thats not happening to me
Yeah when I put your script in it works. im gonna try and figure out what we did differently
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
no idea why that happens
should be the same