Looking for scripting help

Hey DevForum!
I ended up making a script that would essentially “trip” the player, or make them “slip and fall”. Basically I was planning on using math.random to choose a number between 1 and 50, and if the number was 15 then it would put the player into a ragdoll state. It ended up not working, here’s the output :

The script was placed into StarterCharacterScripts
This was the script :

local humanoid = character:WaitForChild("Humanoid")
local randomNumber = math.random(1, 2)
print (randomNumber)
if randomNumber == 1 then
if humanoid:GetState() == Enum.HumanoidStateType.Running then
	Enum.HumanoidStateType.Ragdoll = true
		elseif Enum.HumanoidStateType.Landed then	
		Enum.HumanoidStateType.Ragdoll = false
		
end
	
end```

Looking for help, could anyone explain what I did wrong?

That’s not how you use HumanoidStateTypes.

Humanoid:ChangeState(Enum.HumanoidStateType.Ragdoll)

Some of your code is also generally wrong. The condition of the elseif statement checks only if Enum.HumanoidStateType.Landed exists, not if the Humanoid has that state. In order to combat this, you should hold the Humanoid’s state in a variable and assign based on the value.

Here’s some improved code for you. You were on the right track but made a few mistakes.

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local currentState = humanoid:GetState() -- You'll probably use it later too
local randomNumber = math.random(1, 2) -- I suggest the Random object

if randomNumber == 1 then -- I smell nested if statements coming up here
    if currentState == Enum.HumanoidStateType.Running then
        humanoid:ChangeState(Enum.HumanoidStateType.Ragdoll)
    elseif currentState == Enum.HumanoidStateType.Landed then
        -- Change to another state, maybe. Won't finish this bit.
    end
end
4 Likes