Realistic Reloading Problem

So i was trying to make a realistic reloading like taking out the magazine and bolting the gun and i wrote the code below it is not working https://gyazo.com/079fae2c83d967b0ee8eb66fef9f310f

In the Video I Press R then you see an error in the output you can inspect my code to tell me if i did anything wrong or if this wont work at all.

local UIS = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local char = plr.Character



UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.R then
		char["Right Arm"].CFrame = CFrame.new(-90,2,-191)
		wait(0.001)
		script.Parent.Mag.CFrame = CFrame.new(-90.042,-0.168,-193.32)
		wait(0.001)
		char["Right Arm"].CFrame = CFrame.new(-90,3,-191)
		wait(0.001)
		script.Parent.Mag.CFrame = CFrame.new(-90.042,2.332,-193.32)
	end
end)

that is the code i wrote

Yup like showned in the error, u spelled RightArm wrong, (u did that space there)

1 Like

Make sure you don’t use ‘char’ as a variable name as that is a reserved keyword in Lua.

3 Likes

This error is saying that char does not exist. It is almost as if you are doing num = nil.money. This may be related to what @CAP7A1N said.

Thank you all for the feedback I will be looking into what you guys have told me

So if I change char will that fix the whole thing or

Also, not sure if this might be better but try making an animation and playing it when the gun is reloaded?

I will make an animation when I have this down that is anyway

1 Like

Ok, I was just suggesting because your code makes the right arm telport to the new positions rather than it moving to those positions

Don’t understand why the intervals between the changes in the different positions are so small… anything under 0.03 defaults to 0.03, so honestly just do wait() if you’re so eager about it!

1 Like

Yeah I was just think about it I’ll just use vector3 Instead

Ok thanks for tip now I know anything under 0.03 defaults to 0.03

The character probably isn’t loaded in yet, so try
local char = plr.Character or plr.CharacterAdded:Wait()

First, what everyone else said is true. The character takes time to load + while using char will work, you shouldn’t use keywords as variable names. The default time for waiting is (1/30), which is ~0.033333.... That’s because you can’t wait shorter than a frame, and the wait function runs at 30fps.

Secondly, changing the position will have almost exactly the same effect. What you should do is modify the Motor6Ds instead, which controls a character’s parts. An animation does this automatically, which makes stuff like this so much easier to use.

2 Likes

Ok thank you so much I’ll make an animation first so then all I have to do is change the position of the mag