Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How Can i locate a player in the workspace?

i am trying to find the player in the workspace but it only works in local scripts. can you help me try to locate it regular scripts.

Failing code in regular script

local player = game.Players.LocalPlayer.Name --Find Player name
local nameofplayer = game.Workspace:FindFirstChild(player) --Get the player in workspace
script.Parent.MouseButton1Click:Connect(function()
	nameofplayer.Head.BrickColor = BrickColor3.Blue()

4 comments

0

methods may differ for finding the local player in a server script. Can you please provide some code to work with, or an explanation

0

Done

0

Player.Character is the legitimate method... The LocalPlayer is only attributed to Client Instances, so of course the Server is incapable of reference an Object of it's kind.

0

Whenever i use that in the script it responds with "attempt to index nil with 'Character'"

2 answers

You should probably handle GUIs with Localscripts. You can however change the color of the head on the server using Remote Events

You can find more about remote Events and functions here: https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

Insert a remote event in ReplicatedStorage and insert a regular script into ServerScriptService

heres the Localscript: (put it in the button)

local player = game.Players.LocalPlayer
local character = player.Character -- finds the player in the workspace
local event = game.ReplicatedStorage.	-- type the name of the remote event here

script.Parent.MouseButton1Click:Connect(function()
	event:FireServer()

put this in the regular script in ServerScriptService:

local event = game.ReplicatedStorage. -- type the name of the remote event here

event.OnServerEvent:Connect(function(player)
player.Character.Head.BrickColor = -- color here

Sorry if this is incorrect. I'm kinda new to scripting too

4 comments

0

there are no errors posted but nothing happens

0

is the localscript inside a textbutton in StarterGui?

0

Im using a regular script and its inside of a text button

0

you need to use a localscript rather than a regular script. Just insert a localscript into the button and paste the 1st code block above (make sure to edit where the green text is to your liking)

This discussion is preserved as it was. New answers, comments, and votes are closed.