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

Why does Humanoid.FloorMaterial only update when I jump?

Trying to update a value when the player hits the floor, and it only happens when I press the space key. Can someone help??

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded():Wait()
local Humanoid = Character.Humanoid
local Root = Character.HumanoidRootPart
local rs = game:GetService("RunService")

diving = 2

game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
		    if inputObject.KeyCode == Enum.KeyCode.LeftShift and diving == 0 and Humanoid.FloorMaterial == Enum.Material.Air then 
		print ("Dive!")
		Humanoid.MaxSlopeAngle = 5
		Humanoid.JumpPower = 0
		Humanoid.WalkSpeed = 50
		diving = 1
	end	
end)
game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
	
	local Player = game.Players.LocalPlayer
	local Character = Player.Character or Player.CharacterAdded():Wait()
	local Humanoid = Character.Humanoid
	local Root = Character.HumanoidRootPart
	local rs = game:GetService("RunService")
	
	
if Humanoid.FloorMaterial ~= Enum.Material.Air and diving == 1 then
		print ("Hit ground!")
		Humanoid.WalkSpeed = 0
		Humanoid.JumpPower = 0
		diving = 2

	else if Humanoid.FloorMaterial ~= Enum.Material.Air and inputObject.KeyCode == Enum.KeyCode.Space and diving == 2 then --[Humanoid.MaxSlopeAngle == 5]
	print ("Canceled dive!")
		Humanoid.JumpPower = 60		
		Humanoid.MaxSlopeAngle = 50
			Humanoid.WalkSpeed = 30
			diving = 0
		end
	end
end)

1 answer

✓ Accepted answer

it's only updating when you press space because you're only changing it when InputBegan fires

just move the dive cancel into the other InputBegan, then change the second to a GetPropertyChangedSignal for FloorMaterial

btw generally you shouldn't have to put several InputBegan connections in a single script, unless you need to disconnect one of them later

-- this should be in StarterCharacterScripts instead of StarterPlayerScripts since it relies a lot on the character
-- i don't actually know where it was parented but the lines of code that reset all the variables at the top made it seem like it was in StarterPlayerScripts :^))

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait() -- no idea what the () after CharacterAdded was about
local Humanoid = Character.Humanoid
local Root = Character.HumanoidRootPart
local rs = game:GetService("RunService")

local diving = 2 -- global variables bad

game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.LeftShift and diving == 0 and Humanoid.FloorMaterial == Enum.Material.Air then 
        print ("Dive!")
        Humanoid.MaxSlopeAngle = 5
        Humanoid.JumpPower = 0
        Humanoid.WalkSpeed = 50
        diving = 1
    elseif inputObject.KeyCode == Enum.KeyCode.Space and diving == 2 and Humanoid.FloorMaterial ~= Enum.Material.Air then
        print ("Canceled dive!")
        Humanoid.JumpPower = 60     
        Humanoid.MaxSlopeAngle = 50
        Humanoid.WalkSpeed = 30
        diving = 0
    end 
end)

Humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
    if Humanoid.FloorMaterial ~= Enum.Material.Air and diving == 1 then
        print ("Hit ground!")
        Humanoid.WalkSpeed = 0
        Humanoid.JumpPower = 0
        diving = 2
    end
end)

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