Need help with detecting keyup/keyrelease events

Hi yall, currently im messing around with altering the custom physical properties of a part underneath the player when a key is pressed and held down.

print("slip n slide baby")
wait(5)
print("started up")
local UserInputService = game:GetService("UserInputService")


local keyisdown = false


local DefaultPhysicalProperties = PhysicalProperties.new ( 0.7, 0.3, 0.5, 1, 1 )

local SlipperyPhysicalProperties = PhysicalProperties.new ( 0.7, 0, 0.5, 100, 1 )
--workspace.Part.CustomPhysicalProperties = SlipperyPhysicalProperties

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		keyisdown = true
		print("true")
	end
end)

while keyisdown == true do
	ray = Ray.new(game.Players.LocalPlayer.Character.HumanoidRootPart.Position, Vector3.new(0,-5,0))
	local objecthit = workspace:FindPartOnRay(ray)
	workspace.Part.CustomPhysicalProperties = SlipperyPhysicalProperties
	print("we slippery now")
	print(objecthit, hitposition,normal,material)
end

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		ray = Ray.new(game.Players.LocalPlayer.Character.HumanoidRootPart.Position, Vector3.new(0,-5,0))
		local objecthit = workspace:FindPartOnRay(ray)
		workspace.Part.CustomPhysicalProperties = DefaultPhysicalProperties
		print("we non slip now")
		print(objecthit, hitposition,normal,material)
	end
end)

Currently theres no errors, but I dont know how to escape the UserInputService.InputBegan to listen for the true/false event in the while loop to actually change the custom physical properties

edit cleaned up non-related code

You can just use FloorMaterial to check the surface the player is walking in.

local Humanoid = route.to.humanoid
 
Humanoid:GetPropertyChangedSignal("FloorMaterial"):Connect(function()
    print("New value for FloorMaterial: " .. tostring(Humanoid.FloorMaterial))
end)

Im not currently checking for what surface the player is currently standing on, thats some legacy from a bit of code I was using for guidance on changing a custom physical property of a part thats detected under the player based on which material it is. Im just trying to use the part of it that detects the part underneath, I probably should of cleaned up the material check before posting this.

I need help with using a while loop while a player has a key pressed incase the player goes over multiple parts so it can change them as the player moves

is the code I was using for guidance

I wouldn’t use while loop for this but rather Runservice.RenderStepped and inside it just check if UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) and then compute the necessary rays.

Okay, so I implemented this.

local UserInputService = game:GetService("UserInputService")
local keydown = false
local RunService = game:GetService("RunService")
wait(5)
print("started")

local DefaultPhysicalProperties = PhysicalProperties.new ( 0.7, 0.3, 0.5, 1, 1 )

local SlipperyPhysicalProperties = PhysicalProperties.new ( 0.7, 0, 0.5, 100, 1 )
UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		keydown = true
		print("true")
	end
end)
UserInputService.InputEnded:Connect(function(input)
	keydown = false
	print("false")
end)

RunService.RenderStepped:Connect(function()

	if keydown == true then
		local ray = Ray.new(game.Players.LocalPlayer.Character.HumanoidRootPart.Position, Vector3.new(0,-5,0))
		local objecthit = workspace:FindPartOnRay(ray)
		objecthit.CustomPhysicalProperties = SlipperyPhysicalProperties
		print("we slippery now")
	end
	if keydown == false then
		local ray = Ray.new(game.Players.LocalPlayer.Character.HumanoidRootPart.Position, Vector3.new(0,-5,0))
		local objecthit = workspace:FindPartOnRay(ray)
		objecthit.CustomPhysicalProperties = DefaultPhysicalProperties
		print("we non slip now")
	end
end)

But now if I hold the key for more than say 5 seconds it will lose that the player is holding down the shift key

Don’t check for keydown == true or false in the RenderStepped but rather check for UserInputService:IsKeyDown(Enum.KeyCode.LeftShift)

Sorry to keep bothering you, I appreciate the help.

So now my code looks like this

local UserInputService = game:GetService("UserInputService")
local keydown = false
local RunService = game:GetService("RunService")
wait(5)
print("started")

local DefaultPhysicalProperties = PhysicalProperties.new ( 0.7, 0.3, 0.5, 1, 1 )

local SlipperyPhysicalProperties = PhysicalProperties.new ( 0.7, 0, 0.5, 100, 1 )


RunService.RenderStepped:Connect(function()
	
	UserInputService.InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.LeftShift then
			keydown = true
			print("true")
		end
	end)
	UserInputService.InputEnded:Connect(function(input)
		keydown = false
		print("false")
	end)
end)


while keydown == true do
	wait(1)
	local ray = Ray.new(game.Players.LocalPlayer.Character.HumanoidRootPart.Position, Vector3.new(0,-5,0))
	local objecthit = workspace:FindPartOnRay(ray)
	objecthit.CustomPhysicalProperties = SlipperyPhysicalProperties
	print("we slippery now")
end
while keydown == false do
	wait(1)
	local ray = Ray.new(game.Players.LocalPlayer.Character.HumanoidRootPart.Position, Vector3.new(0,-5,0))
	local objecthit = workspace:FindPartOnRay(ray)
	objecthit.CustomPhysicalProperties = DefaultPhysicalProperties
	print("we non slip now")
end

Im still having the problem where after a second or so it does not recognize that the shift key is still currently being held

local UserInputService = game:GetService("UserInputService")
local keydown = false
local RunService = game:GetService("RunService")
wait(5)
print("started")

local DefaultPhysicalProperties = PhysicalProperties.new ( 0.7, 0.3, 0.5, 1, 1 )

local SlipperyPhysicalProperties = PhysicalProperties.new ( 0.7, 0, 0.5, 100, 1 )


RunService.RenderStepped:Connect(function()
    if UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) then
		local ray = Ray.new(game.Players.LocalPlayer.Character.HumanoidRootPart.Position, Vector3.new(0,-5,0))
		local objecthit = workspace:FindPartOnRay(ray)
		objecthit.CustomPhysicalProperties = SlipperyPhysicalProperties
		print("we slippery now")
     else
		local ray = Ray.new(game.Players.LocalPlayer.Character.HumanoidRootPart.Position, Vector3.new(0,-5,0))
		local objecthit = workspace:FindPartOnRay(ray)
		objecthit.CustomPhysicalProperties = DefaultPhysicalProperties
		print("we non slip now")
	end
end)
1 Like

Thanks a lot, that works perfectly(mostly) and I see what I was doing wrong now.

Incase anyone else comes across this thread the final fix needed to make this work is a replacing

local objecthit = workspace:FindPartOnRay(ray)

with

local objecthit, hitposition,normal,material = workspace:FindPartOnRay(ray, game.Players.LocalPlayer.Character, false, true)

which ignores the player (idk why) and targets the part needed to be made slippery