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

How to Detect if a player has hit the ground?

So I'm doing stuff with jumping mechanics as seen here:

UserInput = game:GetService("UserInputService")

UserInput.InputBegan:Connect(function(KeyDown, gameProcessed)
	if not gameProcessed then
		if KeyDown.KeyCode == Enum.KeyCode.Space then
			wait(0.2)
			game.Workspace.Gravity = 88
			wait(2)
			game.Workspace.Gravity = 110
		end
	end
end)

When they hit the Space bar it will do something, but I want it where the Gravity will be 88 until the player hits the ground, once they hit the ground it will reset it to 110. I know I would need to use the until statement or something, but I just don't know how to detect if a player hits the ground or not.

[P.S, READ I'm also not talking about terrain material, I'm talking about parts. I'm using parts as my terrain.]

1 comment

0

I don't know if this works, but maybe you could use the 'Landed' humanoid state type and when it happens, change the gravity. https://www.robloxdev.com/api-reference/enum/HumanoidStateType

1 answer

✓ Accepted answer

Place all your parts that make up your terrain as a grouped model and name the model as TerrainParts. If so, then:

local terrain = workspace.TerrainParts:GetChildren()

for _, part in pairs(terrain) do
	part.Touched:Connect(function(hit)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid then
			workspace.Gravity = 110
		end
	end)
end

Now that we have compiled the script that detects when the character has touched the ground, we must figure out how to incorporate into your script:

UserInput = game:GetService("UserInputService")

UserInput.InputBegan:Connect(function(KeyDown, gameProcessed)
	if not gameProcessed then
		if KeyDown.KeyCode == Enum.KeyCode.Space then
			wait(0.2)
			game.Workspace.Gravity = 88
			local terrain = workspace.TerrainParts:GetChildren()
			for _, part in pairs(terrain) do
				part.Touched:Connect(function(hit)
				local humanoid = hit.Parent:FindFirstChild("Humanoid")
					if humanoid then
						workspace.Gravity = 110
					end
				end)
			end
		end
	end
end)

13 comments

0

HumanoidStateType events are way more efficient than this method

0

Properties have precedence over children. meaning "local terrain" is just referring to the Terrain property of Workspace, which holds the Terrain class itself.

0

Thanks for the help!

0

@Vulkarin what if the character lands on a building e.g. and not the ground? That is undesirable according to the question asked. @incapaz you're right, made the changes necessary to avoid that conflict. @Shinjaa no problem!

0

Even if the question only wanted the ground to cause gravity changes instead of buildings (which it did not specify), it would be more efficient to fire a ray or use a touched event on the player's legs than an endless amount of Touched events on every part

1

@Vulkarin with all due respect, your "efficiency" wouldn't make a difference in a Roblox game with finite terrain parts. We're not modeling the terrain of an entire country. Although, raycasting may be an alternative solution, it wouldn't make a difference in terms of lag/efficiency/etc. The above script simply just adds the Touched event to all parts that make up the ground; in other words, does

1

not put a cost to the computer resources. But again, you did not post up an answer to his question.

0

Yes, efficiency does make a difference. You don't know how big his game is to begin with, should he have a ridiculous amount of parts than this script will cause nothing less than a ridiculous amount of lag...now imagine, that he begins to write every script in his game with this lazy mindset. Suddenly, his entire game is too laggy to play because remember, a bunch of small things add up to be big

0

I didn't post an answer because LawlR's comment should already be enough to work on. This website isn't about giving scripts directly to people, it's about giving them hints and letting them work through it themselves...yes, I could have just posted it, but I would prefer a hint in the right direction like LawlR's comment

1

You could just add a transparent brick that is close to the ground if that was the case...

1

I'm pretty sure this doesn't cause any lag, it's not doing any large amounts of processing. All it's doing is attaching the Touched event and the function to each part. That's analogous to assigning a value to each object. The lag and other issues arise when the character has a billion parts that touches the ground simultaneously. This script would also run locally so it's not contributing lag to

1

the whole server.

0

Listening to every single part's touched event doesn't cause any lag?? Why are you even arguing with me...your answer is undoubtedly less reliable, less efficient, and overall just lazy. Stop trying to defend it with "well it might not cause lag", it does and will continue to cause lag as the game grows.

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