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.
Is there a way to find the player without the local script LocalPlayer?
For this specific scenario using a tool you can use Players | Roblox Creator Documentation to get the player.
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,
then when the game is loaded it will look like
So you can reference the player (even though it’s a little messy) by saying
local Player = script.Parent.Parent.Parent.Parent
It’s not a local script so he doesn’t have access to Players.LocalPlayer
make a game.Players.PlayerAdded Event
local Player = game.Players:GetPlayerFromCharacter(Character)
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.
I’ve already gave the right answer to the guy. Se manca mlk
Use a :WaitForChild() preferably whenever getting a player’s character.
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.
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.