Getting character from Server Script

I want to be able to create a part from a keypress script how would I go about this? My current issue is making a keypress work in a server script(if even possible…doubtful. So another method is welcomed) and finding the character for the part to parent.

Script that I tried to find the local character:
wait(10)

local function getPlayerFromCharacter(character)

for _, player in pairs(game:GetService("Players"):GetPlayers()) do

if player.Character == character then

print(player.Character.Name) --Printing Character to see if the script works

return player

end

end

end

11 Likes

There’s a method in the Players service called GetPlayerFromCharacter. Use that!

Ex:

local function doSomething(character)
    local player = game:GetService"Players":GetPlayerFromCharacter(character)
    -- do something
end
14 Likes

What do you mean by “keypress”? Do you need to do something on the server when the player presses a key? You should use remote events to accomplish this.

1 Like

You should stop trying to get the local player on the server, as it’s impossible.

Local player refers to the player on whose PC’s the current LocalScript is running on.
Server scripts run on the roblox servers. They don’t have a LocalPlayer, they see all players the same way.
If you want to do something when a player presses the key, you should use a LocalScript and RemoteEvents to communicate with the server script.

Example:

--Server script--
local remote = game.ReplicatedStorage.RemoteEvent
remote.OnServerEvent:Connect(function(plr)
    local p = Instance.new("Part")
    p.Parent = plr.Character
end)
--LocalScript--
local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent")

game:GetService("UserInputService").InputBegan:Connect(function(iobj, gp)
    if gp then return end
    if iobj.KeyCode == Enum.KeyCode.P then
        remote:FireServer()
    end
end)
26 Likes

Range of Solutions


Unfortunately, I cannot provide an accurate solution without the definition of keypress.

Server script upon join:

game:GetService("Players").PlayerAdded:Connect(function(player)
     -- code, player is the player
end)

Server script through method: used for instance in .Touched events

local player = game:GetService("Players"):GetPlayerFromCharacter(character)

Server script through remote event:

remoteEvent.OnServerEvent:Connect(function(player)
     -- code, player is player and is always player in first parameter
end)

Local script:

local player = game:GetService("Players").LocalPlayer

Getting the Character


After we find the player, we need to find the character.

game:GetService("Players"):Connect(function(player)
     player.CharacterAdded:Connect(function(character)
         -- character is character
     end)
end)

If it’s not upon join. Use this for other cases.

local character = player.Character or player.CharacterAdded:Wait()
15 Likes

Where should I put the localScript and the Server script? Apologies but I’m relatively new to this.

For example into the StarterPlayerScripts. And the server script into ServerScriptService.

1 Like

LocalScripts should be in either in these:

  • Tools
  • StarterPlayerScripts – recommended for this case
  • StarterCharacterScripts
  • PlayerGui
  • PlayerScripts
  • ReplicatedFirst – note, loading screens and things prior to game loading, replicates first

Server scripts, in the other hand belongs in ServerScriptService. Sometimes they can be in workspace.

4 Likes

Thank you for the breakdown. I can’t mark more than one thing as the solution however, I’m thankful that you broke this down! I’ll reference this with the example in making remote events for things in the future

1 Like

Can’t reply to two in one message which is mildly irritating but thank you for the help! I greatly appreciate it.

There’s a cool feature in the DevForums, which involves with something like this:
@Operatik

Better than having separate messages. :+1:

1 Like

Oof. I feel stupid for not thinking of mentioning. Thanks anyways, sorry for the spam in that case and I’d better stop replying now. Have a good day though!

First step is that you could delete and add only “Script” and not “Local Script” because it is possible only on the running owner’s PC.

Second step is that the server scripts are run on the roblox servers that will be like “LocalScript”. at the moment it is impossible to execute it in an inappropriate way

1 Like

Please maintain a single thread for your questions, rather than reposting your questions. You already posted about this earlier, though only slightly changed the wording.

Didn’t get notified about that topic at all and it took days to pend which is why I presumed it was rejected for some odd reason. Apologies, I’m taking that topic down now.(flagged for moderator attention)
(My notifications show up as empty and my posts don’t show that topic either.)