So I’m trying to test out a mechanic for a game I’m making
I’m trying to make it so that when you type something into this box and press the button next to it
something will happen in-game depending on what you typed.
I’m trying to do it through remoteevents, if theres an easier way, please tell me.
currently this is the code I have:
Local script in the button:
local reps = game.ReplicatedStorage
script.Parent.MouseButton1Click:Connect(function()
reps.why:FireServer()
end)
Script in the serverscriptservice:
local playerui = game.Players.LocalPlayer.PlayerGui
local reps = game.ReplicatedStorage
reps.why.OnServerEvent:Connect(function(player)
local textchange = playerui.ScreenGui.Frame.TextBox.Text
if textchange == "testing" then
print("works")
end
end)
Ok so…
First of all The server script cannot access LocalPlayer, so to fix this one you’d want the playergui variable to be inside the OnServer event and instead of game.Players.LocalPlayer you would only do player(the argument set inside the OnServerEvent, which you wrote)
Second, the local script will fire and all, but you want to also send the text box’s text within the FireServer call, otherwise the server will NOT know what the player wrote down, due to it being visible only on the player’s screen.
Now lastly. reps.why.OnServerEvent:Connect(function(player, addTextArgumentVariableNameHere)
The “addTextArgumentVariableNameHere” is to be the text you send over the FireServer brackets.
You need to learn that client and server things don’t always exist together. Server doesn’t know what you’re typing.
local playerui = game.player.PlayerGui
local reps = game.ReplicatedStorage
reps.why.OnServerEvent:Connect(function(player,textchange)
local textchange = playerui.ScreenGui.Frame.TextBox.Text
if textchange == "testing" then
print("works")
end
end)
the local script is unchanged
is there anything i did wrong?
game.player doesnt exist
the server does not know there are any players in the game by default
all it has is the list of player objects in the Players service, which is everyone in the server
you can never know what player is firing that event without using the built in player object that is passed in as the first argument
every remote event has a player passed in as the first argument, so thats what you use
BUT, the server CANNOT access the player gui of any player. (the server can access it but its not a good idea to edit anything or read anything to/from it)
since your new function already has the player, and textchange arguments, you just need to check textchange and remove everything else from the function
local reps = game.ReplicatedStorage
reps.why.OnServerEvent:Connect(function(player,textchange)
if textchange == "testing" then
print("works")
end
end)