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

script isn't finding variable declared in LocalScript?

Asked by
Fieu 2
5 years ago

Here is the LocalScript located inside of a button

script.Parent.MouseButton1Click:Connect(function()
    local owner = "Player"
    player = game.Players.LocalPlayer.Name
    game.ReplicatedStorage.RemoteEvent:FireServer()
    print("remote fired!")
    print(player)
end)

The script triggers a RemoteEvent which connects to a script in ServerScriptService, telling the Tutorial GUI to close.

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function()
    print("Close button loaded")
    button = game.Players.player.PlayerGui.Tutorial.Intro.No
    window = game.Players.player.PlayerGui.Tutorial
    function onClicked(GUI)
    window:remove()
    end
    game.Players.player.PlayerGui.Tutorial.Intro.No.MouseButton1Click:connect(onClicked)
end)

However I get this error when I press the button:

player is not a valid member of Players

Here is where the error is located: button = game.Players.player.PlayerGui.Tutorial.Intro.No

So I guess the variable can't be placed in a script that way? When I tried replacing "player" with my own username it worked. There's something wrong I did with the variables.

0
Hi. The reason your code isnt working is well because you stated that player which isnt localized into a string value. At the top of where owner is change owner to local owner = game.players.localplayer.Name Kaosinfusedknight 36 — 5y
0
^ the comment above mine is incorrect. Ignore it. User#21908 42 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

No, you do NOT need a remote event to do this at all.

-- Under button, LocalScript
local player = game.Players.LocalPlayer    

script.Parent.MouseButton1Click:Connect(function() 
    player.PlayerGui.Tutorial:Destroy() -- remove is deprecated, use Destroy
end)
0
Lol I said the same thing as you, but with no code. User#21908 42 — 5y
0
Thanks, the reason why I used a remote was because my original script was only working in test mode and I thought the culprit was FilteringEnabled. And then I thought you needed a remote to do everything involving filteringenabled and localscripts *facepalm* Fieu 2 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

Ok so there are a few problems I see but I will address two. Number one: Closing a tutorial can be handled on the client. As long as it does not affect the player's standing in the game or money or whatever, the client can handle it. If I were a hacker, would I be saying: "yay I can open and close this tutorial whenever I want!"? I don't think so. If it is safe for the client to do it, let the client do it. Second problem: even if the player variable was able to be accessed by the ServerScript(which it is not) your code would not work. The reason is that you have set player to equal a string, which is the player's Name. You cannot do: game.Players."playerName". One thing you could do is use :FindFirstChild(player). I hope this helps and have a great day scripting!

Answer this question