How to find PlayerScripts with ClickDetector?

I am trying to create a block in workspace that whenever you click it, something changes in the script that is in the playerscripts.

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	plr.PlayerScripts.Main.Target.Mesh.MeshType = Enum.MeshType.Sphere
end)

for some reason, it doesn’t detect the playerscripts when I click it, and instead shows an error that states PlayerScripts is not a valid member of Players.MyName. Does anyone know why this is happening? I’ve made scripts in the past that add a tool to a player’s backpack through a workspace button, so I don’t know what the problem is.

I’m not changing any UI. It’s just a part with a script under the workspace, accessing something in PlayerScipts.

I’m changing something that’s parented UNDER the script, not inside of the script itself.

What is that thing? Provide some screenshots so we could see…

The target is ‘that thing’.

Script - Roblox Studio 3_27_2022 1_04_25 AM (2)

Is this written in a local script?

This is inside of a ServerScript.

You’re unable to see PlayerScripts, or at least reference it the way you have on the server to my knowledge. Below is what you’d see on your server for the player:

image

Instead, on MouseClick you can fire to the client, which will allow you to reference PlayerScripts.

I meant the script was a server script, not inside of ServerScriptService.

image

1 Like

Is this screenshot from the client, or the server?

My bad, it’s from the client.

You are probably right, he can’t access that from the server

1 Like

You can’t access that from the server, you could insert a folder instead when the player joins, and parent the local script into that folder

Then how would I access it? 30

As I said, you could insert a folder into the player when he joins, and then parent the local script into it.

Or make a clone of that local script and then parent it into that folder.

And then you’d be able to access from that folder.

You should do what @alphadoggy111 said, have a RemoteEvent with a server script and a local script. The server script fires to the remote event when a player presses the button, and the hooked up local script executes the code to modify PlayerScripts.

I just now saw @alphadoggy111’s edit, I agree with that way,
if you want to access from that folder [PlayerScripts], do what he said.

If you want an alternative way[through a diff folder], you can do what I said.

local clickPart = workspace:WaitForChild"Part"
local clickDetector = clickPart:WaitForChild"ClickDetector"

local function onClicked(player)
	local playerScripts = player:WaitForChild"PlayerScripts"
end

clickDetector.MouseClick:Connect(onClicked)

Local script inside StarterPlayerScripts. No RemoteEvent required.

Hey, I have noticed that you don’t put “()” outside of the instances, is it a typo or is it working like that aswell?

https://www.lua.org/pil/5.html

If the function has one single argument and this argument is either a literal string or a table constructor, then the parentheses are optional.

1 Like