My key holding script isn't working

Hello developer,

i’ve made a key system like piggy and teddy. dit is het script dat ik gemaakt heb

script.Parent.MouseClick:Connect(function(player)
local character = player.Character
local key = script.Parent.Parent

if character:FindFirstChild("Key") then
	local oldKey = character:FindFirstChild("Key")
	oldKey.Parent = workspace
	oldKey.WeldConstraint:Destroy()
	oldKey.CFrame = key.CFrame * CFrame.Angles(0,0, math.rad(45))
end

key.Parent = character
key.CFrame = character.RightHand.CFrame * CFrame.Angles(math.rad(90),0,math.rad(180))

local weld = Instance.new("WeldConstraint")
weld.Parent = key
weld.Part0 = key
weld.Part1 = character.RightHand

end)

This is what I wanted to achieve

mqdefault

This is the results

I hope someone can solve this.

Thanks for reading :grinning:

2 Likes

Your script looks fine and it also seems to do what you want as per the picture shown below. If the issue is the angle looking wrong then I don’t think that’s necessarily your script not working but rather a case of not having the correct rotational values you want. Feel free to experiment with different values to see if you can get a key rotation you like.

Personally I wouldn’t try to do any CFraming of the sort and would just use attachments to designate where I want the key to snap when I weld it to the player’s hand. Sometimes I might do this with accessories but in cases where they don’t work or baked-in behaviours butcher my use case I just use a model and weld it directly to the player’s hand. If the latter idea interests you, check here.

Please do be aware that what I’ve written there is not a full code sample, so you should not just copy and paste my code expecting it to work out of the box. You need to do setup yourself to make sure it works with your systems or you can use it as a learning sample to see how I accomplish this.

1 Like

You could use a plug in like Tool Grip Editor to fix where the key sits in the player/characters hand.