I can't get character in function (server)

function getPlayer()
	local char = Tool.Parent
	return game:GetService("Players"):GetPlayerFromCharacter(char)
end

There’s a script in the starterpack
It was originally a tool script, but it was moved.
But I couldn’t get the Character.
somebody help me please.

2 Likes

Where is the script now located.
Without this information we cannot help you, send a screenshot of the hierarchy.

1 Like

가자

2 Likes

All StarterPack content will be copied to the player’s Backpack if I remember correctly.
So you should be able to access it by fetching the Parent twice.

script.Parent.Parent

2 Likes

However, the location of the script is not the best, it should instead be placed in the ServerScriptService and used RemoteEvents or RemoteFunctions.
Here the code will be duplicated for each client instead of being centralized in a single instance.

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

The above code should work.

However, the following capture clearly shows that when 2 clients connect, the code is duplicated. Which in my opinion isn’t best practice but will still work.

image

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.