Dropping custom items on death

I made a code for my key to drop when a character dies, it is working fine but the problem is if the character were in closed space the key would get stuck in the wall, anyone knows a way around this?
Like maybe make it force spawn in open space when a character dies.

My code to preserve the key when someone dies:

	if character:FindFirstChild("key") then
		character.key.CanCollide = true
		if character.key:FindFirstChild("WeldConstraint") then
		character.key.ProximityPrompt.Enabled = true
		character.key.Parent = workspace

You could use GetPartBoundsInBox (WorldRoot | Roblox Creator Documentation) to see if the key is intersecting with anything. If it is you could raycast at it from 100 studs up to see where the ground is. You could then move the key to that position + half of the parts y size. If you need any help with coding this please ask.

It would be helpful to help me code it :slight_smile:

What is CanQuary and can I use it to achieve this?

CanQuery is a property for base parts that allows you to decide if a part is detected in spatial queries(things like raycasts and getting the parts in a specific area). I think you would be OK without using GetPartBoundsInBox.

local result = workspace:RayCast(character.key.Position+Vector3.new(0,100,0), character.key.Position - Vector3.new(0,100,0)
character.key.Position = result.Position

It is giving me an error, I will check it later maybe next day.

i think it would be best to code your own falling key animation so you could pre-place it in a safe place with saftey-checks

the saftey checks coculd maybe include raycasting to tween the keys antimation to the ground

once animation is done unanchor it

but this just idea u dont have to follow it

I’m sorry guys I think I didn’t explain it well.
What I meant is when the character is holding an item and it died it will drop the item on the same position so if the hand is clipping through a wall it will get stuck there, that is what I wanna fix not when it drops to the ground, but I think @RoloTheDevBunny idea is the solution of GetPartBoundsInBox but I don’t know how to apply it.
here is a reference picture (it is a small cabinet):

I want to detect if the item is clipping into a wall and drop it in the nearest open space where the character dies.

Does it get stuck in walls a lot? If this is a problem you can try setting the key’s position to the characters HumanoidRootPart position.

Thank you, the solution was this simple idk why I didn’t think of that.
Also, I used the CFrame instead of Position to get the correct orientation!

Trying to mark your message as solution, but it gives me error 500

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