Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
RoyMer 301 Moderation Voter
9 years ago

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

Log in to vote
4
Answered by 9 years ago

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)

0
Still didn't work in game :/ RoyMer 301 — 9y
0
Hmm. I'll see what else I can do, I'll try to update as soon as possible! minikitkat 687 — 9y
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!! minikitkat 687 — 9y
0
That was the problem! Thanks!! RoyMer 301 — 9y
0
No problem! minikitkat 687 — 9y
Ad

Answer this question