How can I find the local player without a Local Script?

I’m working on a script where I need to find the player that has the tool in their inventory, and play an animation server side.
image
Is there a way to find the player without the local script LocalPlayer?

2 Likes

For this specific scenario using a tool you can use Players | Roblox Creator Documentation to get the player.

1 Like

I’m fairly certain animations on humanoids replicate to the server regardless of it being called by a localscript or script. Just something I thought should be mentioned.

If your explorer looks a little like this,
image
then when the game is loaded it will look like
image
So you can reference the player (even though it’s a little messy) by saying

local Player = script.Parent.Parent.Parent.Parent
2 Likes

It’s not a local script so he doesn’t have access to Players.LocalPlayer

1 Like

make a game.Players.PlayerAdded Event

2 Likes
local Player = game.Players:GetPlayerFromCharacter(Character)

image

Read the post before commenting.

However to the OP, “Local Player” only exists in local scripts, if you’re just trying to get a player from a tool that’s in a Backpack or a character, I use this:

function getPlayer(s)
	if s.Parent == Players then
		return s
	end
	local plr = Players:GetPlayerFromCharacter(s)
	if plr then
		return plr
	end
	if s.Parent == nil then
		return nil
	end
	return getPlayer(s.Parent)
end

Simple function to get the player from a tool regardless of if it’s equipped or just in the backpack.

1 Like

I’ve already gave the right answer to the guy. Se manca mlk

What you suggested doesn’t work.

1 Like

@CrazyGrapefruit32 This is probably a better alternative to what I previously posted:

Did I do something wrong? I’m confused



Tiny error message/\

Use a :WaitForChild() preferably whenever getting a player’s character.

1 Like

It will be better to just save the animation and maybe the player if he needs it, do the loading of the animation inside the Equipped function

local Player, Anim = nil
script.Parent.Equipped:Connect(function()
	Player = game:GetService("Players"):GetPlayerFromCharacter(script.Parent.Parent)
	Anim = Player.Character.Humanoid.Animator:LoadAnimation(Animation)
end)

Yes,

local Player = script:FindFirstAncestor("Player")

Should be

local Player = script:FindFirstAncestorOfClass("Player")

Apologies for saying that

Hey, I read through this entire thread. Just use a local script. Because all you are doing is playing an animation and welding through motor 6D.

And FYI, the network owner of player’s character is the player itself, and not server. Meaning any changes the local script makes to the player"s character or a a avatar, it gets replicated to the server and all other clients. So use a local script and you should be fine.

2 Likes

You can do a player added function and put player in the parameter that way you have the local player in a server script.

(I’m not sure if this will definitely work, but it should)

Here’s the easiest way, put this inside a Script in StarterCharacterScripts;

–Ensure Players service is loaded
game:WaitForChild(“Players”)
wait(0)

–Lets do this

local Character = script.Parent
local CharName = Character.Name
local Players = game:GetService(“Players”)
local PlayerName = CharName
local Player = Players:FindFirstChild(PlayerName, true) or Players:WaitForChild(PlayerName)

–Ensures all of the above variables are loaded
wait()

–Time for some magic

if Player then
local LocalPlayer = Player
end

(I wrote this on mobile, so I may have missed some things out haha.)

You can easily add this to the top of your existing script to use “LocalPlayer” as a variable inside the script itself.