Local Player Problems

I’m having trouble detecting the local player in this function.

local HairStyleButtonArray = workspace.LOBBY.SettlerLobby.MasterCCDisplay.MainDisplay.GuiMasterParent.HairSelectionMainframe.HairStyles:GetChildren()
local ClickSound = workspace.LOBBY.SettlerLobby.MasterCCDisplay.MainDisplay:WaitForChild("Click")
local Player = game.Players.LocalPlayer

for i , x in pairs(HairStyleButtonArray) do
	if x:IsA("ImageButton") then
		x.MouseButton1Click:Connect(function()
			ClickSound:Play()
			if x.Name == "01HairStyle" then
				local char = Player.Character
	
				if char:findFirstChild("Humanoid") then
					print("Mhm.1")
				end
			end
			if x.Name == "02HairStyle" then
				local char = Player.Character
				
				if char:FindFirstChild("Humanoid") then
					print("Mhm.")
				end
			end
		end)
	end
end

This script is meant to detect when a series of buttons are clicked. It’s meant to differenciate which of the buttons are clicked. Everything in this script works except everything underneath if x.Name == "02HairStyle" then. This line itself works, I deleted local char = Player.Character and had it print something, it manages to differenciate the button just fine. The problem is with the Local Player’s character. I’m having trouble connecting my local “Player” to the LocalPlayer’s character. Note that this is a normal script (as opposed to a local script) in ServerScriptService. Does anyone know the issue with this script?

1 Like

You aren’t able to use LocalPlayer from a server script. Any GUI-based events like button clicks should be handled from a local script.

1 Like

The issue is if I transfer this into a local script, I won’t be able to carry out the true purpose (was going to replace the print(“Mhm”) with a script that changes the player’s hairstyle. This would only be visible to the local player if I put this in a local script.

1 Like

Put this into a LocalScript; but for the function you want done to show the player’s hairstyle, make a server Script connected by a RemoteFunction to trigger it from the LocalScript. The player’s hairstyle does not show from the LocalScript because it is assigned to the LocalPlayer through FilteringEnabled. This is how I would do it, and I hope this gives you a better understanding of how to do it.

1 Like

Just use a remoteEvent to call the print on the server side while having the localplayer code on the client side. Another option is to use PlayerAdded on the server side to get the player;

game.Players.PlayerAdded:Connect(function(plr)

end)
1 Like

I actually tried this but I dont know how to transmit data through a RemoteEvent

1 Like

You need one script, one localscript and a remote event object. I would put the remote event in replicated storage so both scripts can see it. I would put the script in serverscriptservice and the localscript in the playerscripts.

Localscript;

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage.RemoteEvent
event:FireServer("hi")

Server script;

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage.RemoteEvent

event.OnServerEvent:Connect(function(plr, vlaue)
    print(value)
end)
1 Like

And that would print “hi”?
30characterlimit

1 Like

If the RemoteEvent, localscript, and script are in the right spots, it will print “hi” whenever a player joins the game. Every time a new player joins the server, it will print “hi” server side.

1 Like

Alright, gonna try applying this, hold on

1 Like

It works, fired on a RemoteEvent parameter. Thank you very much.

1 Like