hit.Parent help

This is most like a noob mistake so i will allow you all to laugh at me on this one.
i mustnt understand the hit parameter properly as im using one through a remote event to destroy something
i did this already for another function and that one works well
however, in this function it thinks the payer is touching the part when the animation plays which results in everything getting deleted…
here is the function in question

local function destroyBox(hit)
	local PointsCrate = game.Workspace["Level 1"].PointsCrate
	PointsCrate = hit.Parent
	if hit.Parent then
		print("touched")
	else
		print ("not touched")
	end
	if SlideTrack.IsPlaying then
		print("isplaying")
	else
		print("not playing")
	end
end



foot.Touched:Connect(destroyBox)

so the first if statement plays out correctly, it prints “touched” when touching the part
but when isPlaying is checked in the second if statement (when i play the animation) it prints “touched” even when im not touching the part.
im not sure if i have used it wrong since there is another function withthe hit parameter in the same script or if i just havent set the variable correctly, thoughts?

What are you actually trying to do here?

Side note: Read this guide. It would help us to help you.

I don’t understand what exactly you mean, but maybe that’s what you mean

	if hit.Parent and SlideTrack.IsPlaying then
		print("touched")
	else
		print ("not touched")
	end

I do not know what exactly you want to achieve, you need to provide more details

ok lemme try again.
i have a box im trying to destroy when i slide into it
i stripped my code down to check for the issue
the issue is when i slide, it thinks im touching the box when im not
this made it so when the remote event fired, it deleted everything else

i did what u suggest before i posted the script is just rearranged to find the issue which im explaining has something to do with when im sliding

Your problem here is that it is being touched by a base part. You will need to add a check that if a character is touching. For example:

if hit.Parent:FindFirstChildOfClass("Humanoid") then
        -- The player is touching the part
    end

You could also implement a way if the player is named the player who is supposed to touch that crate only.

1 Like

i dont fully understand what you mean since the hit.Parent is the PointsCrate not the Player.
i’ve done exact same thing in another function that kills an NPC that is why im confused.
so to repeat myself here,
i rearranged my code to do seperate checks to find the problem
the first check is to see if i touch the part or not, works as intended only prints when i touch the PointsCrate
the second check is to check whether the anim plays since i have to slide to break it
it is here where it thinks every part is the PointsCrate when i do the animation.

so im not too sure what you mean there

local function destroyBox(hit)
	local PointsCrate = game.Workspace["Level 1"].PointsCrate
	if hit or hit.Parent == PointsCrate then
		print("touched")
	else
		print ("not touched")
	end
	if SlideTrack.IsPlaying then
		print("isplaying")
	else
		print("not playing")
	end
end



foot.Touched:Connect(destroyBox)

This means that you want to remove the frame when the player “slides” into a certain part and for this to happen via a remote event

It would have to work on this principle, if this is not what you mean, try to show some visualization of what you have in mind

  • LocalPlayer
local RS = game:GetService("ReplicatedStorage")

local slidepart = workspace.SlidePart

local RSlide = RS.Slide

slidepart.Touched:Connect(function(destorybox)
	if destorybox.Parent:FindFirstChild("Humanoid") then
		RSlide:FireServer()
	end
end)

  • Server
local RS = game:GetService("ReplicatedStorage")

local boxToDestroy = workspace.DestroyBox

local RSlide = RS.Slide

RSlide.OnServerEvent:Connect(function(destroyBox)
	boxToDestroy:Destroy()
end)

What @gIwwinnq is saying is that your crate is being touched by something else. Your current code is not checking if the player is touching the part, but rather if any part touching the box has a parent and if it does, it will get destroyed. You need to implement a check to see if this part touching the box has a humanoid.

I’m confused what you are trying to convey to us? Could you explain if the “is playing” being repeated multiple times or not? Besides, the slide should be outside of the function once it has been called off since the case is being duplicated many times when touching with hit, may I ask to know what is “foot” assigned to?

Also when the slide track is playing, it may have been due to another base part touching it. That is where you could implement a check there if it is the foot is touching it and also the slide track is playing.

if its being touched by something else then why does the first if statement only print when the player touches the box and not be touched multiple times by the part its on top of?

its when the second if statement comes into play, as soon as the player slides it will print “touched” when u slide on any part. “isPlaying” will only print when the animation plays as intended.
when i slide it thinks everything is the box so if i added the remote event back in it would delete everything else no matter where i was sliding

ive done the exact same thing to kill an npc and that works fine and i did the same code but it doesnt have the same effect

it’s because there’s no collision made between the crate and the part it’s on top of

you misread that one, you are answering out of context

ok im guess i have to show you since words aint doing much for me
if i connect my remote event back to the function, this happens:
robloxapp-20240201-2325120.wmv (872.6 KB)

as i have been saying it thinks everything is the box

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.