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

Why would this keydown not work in game but works in studio ?

This is in the starterpack

local player = game.Players.LocalPlayer

function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.X then
		player.Character.Torso.CFrame = CFrame.new(player.Character.Torso.Position.x,player.Character.Torso.Position.y+30,player.Character.Torso.Position.z)		
	end
end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)

1 answer

✓ Accepted answer

Another simple fix that goes unnoticed by many! Your problem is that this script is firing before the LocalPlayer loads. Therefor the code can't find game.Players.LocalPlayer because it's not there yet! So we need to add a simple loop to yield the script until the LocalPlayer loads.

repeat wait() until game.Players.LocalPlayer --this will allow the rest of the script to activate once the character is fully loaded!

local player = game.Players.LocalPlayer

function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.X then
        player.Character.Torso.CFrame = CFrame.new(player.Character.Torso.Position.x,player.Character.Torso.Position.y+30,player.Character.Torso.Position.z)        
    end
end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)

5 comments

0

Still didn't work in game :/

0

Hmm. I'll see what else I can do, I'll try to update as soon as possible!

1

Actually! Is your script a Local Script or a normal script? If a it is a normal script change it to a LocalScript! LocalPlayer will not work with a normal script!!

0

That was the problem! Thanks!!

0

No problem!

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