Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I need help with my "C" to crouch/crawl?

I'm creating a game that requires you to crouch/crawl, and I tried a script but seems not to fully work. Can anyone help please? Thanks :)

--[ Setup ]--
-- Locations
local Serkisx = script.Parent
local Player = game.Players.LocalPlayer
local Avatar = Player.Character
local Mouse = Player:GetMouse()

-- Emptys
local Hold = nil

--[ Get Items ]--
local Humanoid = Avatar:WaitForChild("Humanoid")
local Torso = Avatar:WaitForChild("Torso")
local RH = Torso:WaitForChild("Right Hip")
local LH = Torso:WaitForChild("Left Hip")

local RL = Avatar:WaitForChild("Right Leg")
local LL = Avatar:WaitForChild("Left Leg")

local RJ = Avatar:WaitForChild("HumanoidRootPart"):WaitForChild("RootJoint")

--[ Functions ]--
function CreateWeld(Part, CF)
	local w = Instance.new("Weld")
	w.Name = "LegWeld"
	w.Parent = Torso
	w.Part0 = Torso
	w.Part1 = Part
	w.C1 = CF
end

--[ Hooks ]--
-- Controls
Mouse.KeyDown:connect(function(Key)
	if (Hold ~= nil) then return end
	if (string.upper(Key) ~= "C") and (string.lower(Key) ~= " ") and  (string.lower(Key) ~= "0") then return end
	Hold = true

	if (Torso:FindFirstChild("LegWeld") == nil) and (string.lower(Key) ~= " ") and (string.lower(Key) ~= "0")then
		-- Right Leg
		RH.Part1 = nil
		CreateWeld(RL, CFrame.new(-0.5, 0.75, 1))

		-- Left Leg
		LH.Part1 = nil
		CreateWeld(LL, CFrame.new(0.5, 0.495, 1.25) * CFrame.Angles(math.rad(90), 0, 0))
		
		-- Lower Character
		RJ.C0 = CFrame.new(0, -1.25, 0) * CFrame.Angles(0, 0, 0)
		RJ.C1 = CFrame.new(0, 0, 0) * CFrame.Angles(0, 0, 0)
		
		-- Slow Walk Speed
		Humanoid.WalkSpeed = 5
	else
		-- Right Leg
		RH.Part1 = RL

		-- Left Leg
		LH.Part1 = LL

		-- Delete Welds
		for i, s in pairs(Torso:GetChildren()) do
			if (s.Name == "LegWeld") and (s.ClassName == "Weld") then
				s:Destroy()
			end
		end

		-- Raise Character
		RJ.C0 = CFrame.new(0, 0, 0) * CFrame.Angles(0, 0, 0)
		RJ.C1 = CFrame.new(0, 0, 0) * CFrame.Angles(0, 0, 0)

		-- Normal Walk Speed
		Humanoid.WalkSpeed = 16
	end

	wait(0.5)

	Hold = nil
end)

1 answer

✓ Accepted answer

The problem was on line 05

local Avatar = Player.Character

which I have changed to

local Avatar = workspace:WaitForChild(Player.Name)

I am not really sure how to use the character property to do this so I have editted the code so that the character is just being accessed using the player name.

Just make sure the script is in a local script and said script is in starter player scripts.

Fixed code:

--[ Setup ]--
-- Locations
local Serkisx = script.Parent
local Player = game.Players.LocalPlayer
local Avatar = workspace:WaitForChild(Player.Name)
local Mouse = Player:GetMouse()

-- Emptys
local Hold = nil

--[ Get Items ]--
local Humanoid = Avatar:WaitForChild("Humanoid")
local Torso = Avatar:WaitForChild("Torso")
local RH = Torso:WaitForChild("Right Hip")
local LH = Torso:WaitForChild("Left Hip")

local RL = Avatar:WaitForChild("Right Leg")
local LL = Avatar:WaitForChild("Left Leg")

local RJ = Avatar:WaitForChild("HumanoidRootPart"):WaitForChild("RootJoint")

--[ Functions ]--
function CreateWeld(Part, CF)
    local w = Instance.new("Weld")
    w.Name = "LegWeld"
    w.Parent = Torso
    w.Part0 = Torso
    w.Part1 = Part
    w.C1 = CF
end

--[ Hooks ]--
-- Controls
Mouse.KeyDown:connect(function(Key)
    if (Hold ~= nil) then return end
    if (string.upper(Key) ~= "C") and (string.lower(Key) ~= " ") and  (string.lower(Key) ~= "0") then return end
    Hold = true

    if (Torso:FindFirstChild("LegWeld") == nil) and (string.lower(Key) ~= " ") and (string.lower(Key) ~= "0")then
        -- Right Leg
        RH.Part1 = nil
        CreateWeld(RL, CFrame.new(-0.5, 0.75, 1))

        -- Left Leg
        LH.Part1 = nil
        CreateWeld(LL, CFrame.new(0.5, 0.495, 1.25) * CFrame.Angles(math.rad(90), 0, 0))

        -- Lower Character
        RJ.C0 = CFrame.new(0, -1.25, 0) * CFrame.Angles(0, 0, 0)
        RJ.C1 = CFrame.new(0, 0, 0) * CFrame.Angles(0, 0, 0)

        -- Slow Walk Speed
        Humanoid.WalkSpeed = 5
    else
        -- Right Leg
        RH.Part1 = RL

        -- Left Leg
        LH.Part1 = LL

        -- Delete Welds
        for i, s in pairs(Torso:GetChildren()) do
            if (s.Name == "LegWeld") and (s.ClassName == "Weld") then
                s:Destroy()
            end
        end

        -- Raise Character
        RJ.C0 = CFrame.new(0, 0, 0) * CFrame.Angles(0, 0, 0)
        RJ.C1 = CFrame.new(0, 0, 0) * CFrame.Angles(0, 0, 0)

        -- Normal Walk Speed
        Humanoid.WalkSpeed = 16
    end

    wait(0.5)

    Hold = nil
end)

If this helped please accept this as the answer.

This discussion is preserved as it was. New answers, comments, and votes are closed.