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
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.
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)
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
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
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.)