Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I get a player's character without using local scripts?

Asked by 3 years ago

I'm doing a coding thing, and I need to get the character from the player, but if I use local scripts it wont show up for other players.

My code is this:

function Clicked(plr) 
plr.Character:FindFirstChild("Accesories"):FindFirstChild("Left Eye").Size = Vector3.new(1,1,1)
end
script.Parent.MouseButton1Down:Connect(Clicked) 

(This is a gui btw)

When I click it, the output says Players.S_Green.PlayerGui.ScreenGui.TextButton.Script:2: attempt to index number with 'Character''

I need help...

3 answers

Log in to vote
1
Answered by 3 years ago

.MouseButton1Down has 2 parameters which is x and y coordinate of the mouse.

So technically your 'plr' is the x coordinate. You need to use remote event for this

Local Script:

local plr = game.Players.LocalPlayer
local event = -- your remote event here

function Clicked()
    event:FireServer()
end

script.Parent.MouseButton1Down:Connect(Clicked)

Server Script:

local event = -- your remote event here

event.OnServerEvent:Connect(function(plr)
    local char = plr.Character
    char:FindFirstChild("Accesories"):FindFirstChild("Left Eye").Size = Vector3.new(1,1,1)
end)
Ad
Log in to vote
1
Answered by 3 years ago

Your real problem is that you need to find a way for the changes to replicate to other players. When working with GUI, your code should always go on the client, in a LocalScript. Think about it, every player has their own unique set of GUIs that are rendered only to them, it wouldn't make sense for the server to handle this for every player.

If you keep it in a LocalScript, you can make sure the changes replicate to other players by using RemoteEvents. By calling FireServer() when your button is clicked, the server will be able to access the player who fired the event so they can change the size accordingly, using the .OnServerEvent event listener.

0
Yes, but then exploiters could take over the server ENTIRELY. And that is YOUR problem for having GUI triggers on the server. Mrdirtbloc -9 — 3y
0
No, this is why you need to code responsibly and have sanity checks done on the server. If you were to avoid using RemoteEvents at all costs for the fear of exploiters, you would have no way to communicate between the client and server. cowsoncows 951 — 3y
Log in to vote
1
Answered by
Elyzzia 1294 Moderation Voter
3 years ago
Edited 3 years ago

for one, MouseButton1Down passes the coordinates that the mouse clicked at, not the player who clicked

and two, putting gui things on the server is bad practice anyway—if you need something to happen on the server when a gui is clicked, detect the input on the client and use a RemoteEvent

-- server
local remote = path.to.RemoteEvent

remote.OnServerEvent:Connect(function(player)
    player.Character.Accessories["Left Eye"].Size = Vector3.new(1, 1, 1) -- you don't need to use FindFirstChild on something that you know already exists
end)

-- client
local remote = path.to.RemoteEvent

script.Parent.MouseButton1Down:Connect(function()
    remote:FireServer()
end)

if you don't understand how remotes work, there's a tutorial for them on the wiki

Answer this question