Ways to get LocalPlayer

Hi,
Often I have problem with getting LocalPlayer in script (cause game.Players.LocalPlayer doesn’t always work and it can be used only in Local Script) I would like to know if there are other better ways to get it.

9 Likes

What exactly is your usecase for needing the localplayer in a server script? You’d probably need to have a remote event up depending on what is needed

2 Likes

I need to use player’s character propeties in mouse keydown

1 Like

If you want to get a player that clicked a button on the server side, you can use remote events.

2 Likes

You’re trying to change the properties server side? You’d need a remote event in that case, here is some more info about it

https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

You’d probably need to fire the remote event when you need to change something and have the remote event set up so when it is fired it’ll change the required property, OnserverEvent automatically gets the player who fired the event, so no need to fire it in yourself

3 Likes
local Event = -- RemoteEvent Path Here
local Mouse = game.Players.LocalPlayer:GetMouse()
local Key = "" -- Key to press here

Mouse.KeyDown:Connect(function(key)
if key == Key then 
Event:FireServer()
end
end)

Server Script:

local Event = -- Path to Remote Event
Event.OnServerEvent:Connect(function(Player)
-- Do stuff
end)
6 Likes

No, I need it in R to Reset Script

Then you should find a way to make that fire a remote, and make the server receive that and kill the player that activated it.

1 Like

Thanks but I wanted to get player

1 Like

Remotes by default send a player object which the server receives as an extra value. You should use that and get the character object of that player.

--client
local repl = game:GetService("ReplicatedStorage")
local rem = repl:WaitForChild("remtest")

remtest:FireServer()
--server
local repl = game:GetService("ReplicatedStorage")
local rem = repl:WaitForChild("remtest")

rem.OnServerEvent:Connect(function(player)
local char = player.Character

--do character magic here like kill or stuff

end)
5 Likes

You don’t need to use RemoteEvent for resetting script.

local player = game.Players.LocalPlayer
local USI = game:GetService("UserInputService")
local actionKey = Enum.KeyCode.R
local debounce = true

USI.TextBoxFocused:Connect(function()
    debounce = false
end)

USI.TextBoxFocusReleased:Connect(function()
    debounce = true
end)

USI.InputBegan:Connect(function(Key)
    if Key.KeyCode == actionKey and debounce == true then
        player.Character.Humanoid.Health = 0
    end
end)

To prevent resetting when the chat bar focused and the player is typing, you can use TextBoxFocused and when the player releases it TextBoxReleased.

3 Likes

You don’t need TextBox stuff, you just need the 2nd return of InputBegan to prevent it from working when typing

local player = game.Players.LocalPlayer
local USI = game:GetService("UserInputService")
local actionKey = Enum.KeyCode.R
local debounce = true

USI.InputBegan:Connect(function(Key, GPE)
    if Key.KeyCode == actionKey and not GPE and player.Character then
        player.Character.Humanoid.Health = 0
    end
end)
3 Likes

you can’t detect user input on the server itself, only via remotes

3 Likes

You could use a server script.

game.Players.PlayerAdded:Connect(function(player)
-- now you can define the player for whatever you need it for.
-- for example:
   player:kick()
   -- or
   player.Character.Humanoid.WalkSpeed = 20
end)

If your trying to get the mouse hit position, you should use a local script to determine when this action should be done, then fire a remote event from a local script to a server script to perform the action by doing YourRemoteEvent:FireServer(Mouse.Hit), and then to catch this “Fire Server”, you have to do YourRemoteEvent.OnServerEvent:Connect(function()

4 Likes

It does get the Player for you.

2 Likes