Player's character
You really shouldn't attempt getting the character by indexing the workspace for the player's name. You should access the player's character by directly indexing the local player, which will get that specific user's character - not whatever the workspace gives you.
Naive approach
The naive approach to handling this would just simply be trying to get the character from the player, without waiting for it to load. Most of the time, the local script loads before the character does. Meaning you need some way to wait for the character to be added to the player, before the script continues.
Lucky for us, there's an event just for this purpose called CharacterAdded! This is how you'd get the character from the player:
local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character
Now here's an example implementing the same method of getting the character, but mixed with an or statement that tells us to wait for the character to be added, then return the character. Here's an example:
local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
That is how you should always handle getting the character in a local script. As far as your UserInputService section goes, everything looks fine. Though, I do have a UserInputService module interface that makes working with UIS a lot easier to use if you're interested. You can find it here. Hope this helped, let me know if you have any questions!
1 comment
It's probably due to line 3 where you wait for the player to load I could be wrong though..