Have problems with my cafe script

Hello, I made a simple testing cafe game.

When I, put cup in recyle bin or handto it to player, My hand stays same, And my body parts has some dicreases too! I am using fixjoints script and refresh command to revoke, it. Is there any way to fix it? I think, this is scripting error.

I tried, changing cup’s parent to starterpack but it didn’t worked, I also found no solutions.

Video is down below.

Script I used for cup:
local Tool = script.Parent
local Handle = Tool:FindFirstChild(“Handle”)
local Equipped = false

local Player = game.Players.LocalPlayer
repeat wait() until Player.Character wait()
local Character = Player.Character
repeat wait() until Character:FindFirstChild("Humanoid") wait()
local Humanoid = Character:FindFirstChild("Humanoid")
local Head = Character:FindFirstChild("Head")
local Torso = Character:FindFirstChild("Torso")
local rArm = Character:FindFirstChild("Right Arm")
local rShoulder = Torso:FindFirstChild("Right Shoulder")
local Neck = Torso:FindFirstChild("Neck")

local Weld, Current
local NeckOriginal = CFrame.new(0, 1, 0, -1, -0, -0, 0, 0, 1, 0, 1, 0)
local ArmOriginal = CFrame.new(-1.4, 0.4, -1) * CFrame.fromEulerAnglesXYZ(math.rad(-90), 0, 0)
local ArmDrink = CFrame.new(0, -0.5, -1.3) * CFrame.fromEulerAnglesXYZ(math.rad(-130), math.rad(-20), math.rad(-60))

local Mem = {
	NeckCF = CFrame.new(0,0,0);
}

function Lerp(A, B, C)
	return A + (B - A) * C
end

function Serp(A, B, C)
	local D = A:Dot(B)
	if D > 0.99999 or D < -0.99999 then
		return C <= 0.5 and A or B
	else
		local R = math.acos(D)
		return (A * math.sin((1 - C) * R) + B * math.sin(C * R))/math.sin(R)
	end
end

function Merp(A, B, C)
	local AX, AY, AZ, A00, A01, A02, A10, A11, A12, A20, A21, A22 = A:components()
	local BX, BY, BZ, B00, B01, B02, B10, B11, B12, B20, B21, B22 = B:components()
	local V0 = Lerp(Vector3.new(AX, AY, AZ), Vector3.new(BX, BY, BZ), C)
	local V1 = Serp(Vector3.new(A00, A01, A02), Vector3.new(B00, B01, B02), C)
	local V2 = Serp(Vector3.new(A10, A11, A12), Vector3.new(B10, B11, B12), C)
	local V3 = Serp(Vector3.new(A20, A21, A22), Vector3.new(B20, B21, B22), C)
	return CFrame.new(V0.X, V0.Y, V0.Z, V1.X, V1.Y, V1.Z, V2.X, V2.Y, V2.Z, V3.X, V3.Y, V3.Z)
end

function Pitch(lookVector)
	return math.atan(lookVector.Y/math.sqrt(lookVector.X^2 + lookVector.Z^2))
end

function Run(F, ...)
	local A
	setfenv(F, setmetatable({wait = function(X)
		delay(X, function() 
			if Current == A then 
				coroutine.resume(A) 
			end 
		end) 
		coroutine.yield() 
	end},
	{__index = getfenv(0)}))
	A = coroutine.create(F)
	Current = A
	coroutine.resume(Current, ...)
end;

function Update(Mouse)
	local PitchMax = 0.1
	local YawMax = 0.2
	local NeckPitch = -Pitch(Mouse.Hit.lookVector)
	local NeckYaw = (Head.CFrame.p - Mouse.Hit.p).Unit:Cross(Torso.CFrame.lookVector).Y
	NeckPitch = NeckPitch/5
	NeckYaw = NeckYaw/5
	if NeckPitch > PitchMax then	
		NeckPitch = PitchMax
	end
	if NeckPitch < -PitchMax then
		NeckPitch = -PitchMax
	end
	if NeckYaw > YawMax then
		NeckYaw = YawMax
	end
	if NeckYaw < -YawMax then
		NeckYaw = -YawMax
	end
	local NeckCF = NeckOriginal * CFrame.Angles(NeckPitch, 0, NeckYaw)
	Neck.C0 = Merp(Mem.NeckCF, NeckCF, 0.5)
	Mem.NeckCF = NeckCF
end

function Drink(Toggle)
	if Toggle then
		local Start = Weld.C1
		for i = 0, 1, 0.1 do
			Weld.C1 = Merp(Start, ArmDrink, i)
			wait()
		end
		if Handle:FindFirstChild("Drink") then
			if Handle.Drink.IsPlaying then
				Handle.Drink:Stop()
			end
			Handle.Drink:Play()
		end
	else
		if Handle:FindFirstChild("Drink") then
			if Handle.Drink.IsPlaying then
				Handle.Drink:Stop()
			end
		end
		local Start = Weld.C1
		for i = 0, 1, 0.1 do
			Weld.C1 = Merp(Start, ArmOriginal, i)
			wait()
		end
	end
end

Tool.Equipped:connect(function(Mouse)
	Equipped = true
	wait(0.1)
	rShoulder.Part1 = nil
	Weld = Instance.new("Weld", Head)
	Weld.Part0 = Head
	Weld.Part1 = rArm
	Weld.C1 = ArmOriginal
	wait(0.1)
	Mouse.Button1Down:connect(function()
		Run(Drink, true)
	end)
	Mouse.Button1Up:connect(function()
		Run(Drink, false)
	end)
	while Equipped do
		Update(Mouse)
		wait()
	end
	rShoulder.Part1 = Player.Character:FindFirstChild("Right Arm")
	Weld:Destroy()
	Neck.C0 = NeckOriginal
end)

Tool.Unequipped:connect(function(Mouse)
	Equipped = false
end)

_
Video:

1 Like

All you need to do it parent to cup to workspace before deleting it so that the Tool.Unequipped event will activate, and add some portion of the deactivated code into it like so:

Tool.Unequipped:connect(function(Mouse)
	Equipped = false
	rShoulder.Part1 = Player.Character:FindFirstChild("Right Arm")
	Weld:Destroy()
	Neck.C0 = NeckOriginal
end)

As this is for a cup, you may have a better time using animations rather than weld based solutions. You can make it so when you drink a cup, your head dynamically moves for a fraction of the code above. Starting animations is as simple as using :Play() and stopping is as simple as :Stop().

This will help your use case above, as upon unequipping, you could prematurely stop the animation in one simple line . You can also place markers in the animation, which can be used to signal a drinking animation upon the cup reaching your mouth or something. There is alot of room for movement and roblox’s animation editor can help you achieve an aesthetically pleasing animation. I suggest you check this out!

More on that here: Animation | Documentation - Roblox Creator Hub

It didn’t work… Can I, still get solutions? I did parented it workspace, But it dosen’t works like that.

3 Likes