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

My fly up 'E' using 'Y' value script isn't working?

The title says it all:

local player = game.Players.LocalPlayer
local character = player.Character
local uis = game:GetService("UserInputService")
local mouse = player:GetMouse()
mouse.KeyDown:Connect(function(key)
	print(key)
	if key == Enum.KeyCode.E then
		character.HumanoidRootPart.Position.Y = character.HumanoidRootPart.Position.Y + 1
	
	end
end)

Thanks!

1 comment

0

Also you dont need to get the mouse if you arent making any changes

2 answers

It's because you didn't use InputBegan a class of UserInputService. It fires when a player has made an interaction with their keyboard. Also mouse.KeyDown is depricated/outdated userinputservice instead

local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
uis.InputBegan:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.E and not gameProcessed then
 character.HumanoidRootPart.Position.Y = character.HumanoidRootPart.Position.Y + 1
				end
			end
		end
	end
end)

8 comments

0

It says attempt to index nil with HumanoidRootPart

0

It must be that position theres isn't an instance called position try using cfame instead.

1

for local character variable, change the line to this: local character = player.Character or player.CharacterAdded:Wait()

0

Ok

0

You have two extra ends.

0

You also don't need to check if the InputType is keyboard.

0

Use "character:SetPrimaryPartCFrame(character:GetPrimaryPartCFrame() * CFrame.new(0, 1, 0))".

0

Youtubemaster I know that but its good practise.

Hello.

Problem

You used Position instead of SetPrimaryPartCFrame().

Solution

Use SetPrimaryPartCFrame(). It sets the PrimaryPart's CFrame.

Recommendations

Use UserInputSrvice as Mouse.KeyDown is deprecated.

Fixed Code (Place it in StarterCharacterScripts)

local UserInputService = game:GetService("UserInputService")
local character = script.Parent

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.E and not gameProcessedEvent then
		character:SetPrimaryPartCFrame(character:GetPrimaryPartCFrame() * CFrame.new(0, 1, 0))
	end
end)

5 comments

0

For some reason it doesn't fly though, it just bounces up then goes back down to the position it was before

0

Sadly that's just how roblox physics work.

0

How could I make it fly up and hover? I thought this script would do that

0

It will not do that. If you had actually told us you wanted it to fly in the original post, it would be better. Do not post non-descriptive questions.

0

I said I was trying to make it 'fly up'

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