Remote Functions

Hello,

In my last topic people said I had to use remote functions, so no exploiters can give themself as much cash as they want.

Now i’ve been trying to use remote functions for my game with help from the developers hub. And I found this script but it gives an error.

This is the error: image

Here’s the script;

-- Server

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local createPopupRequest = Instance.new("RemoteFunction")
createPopupRequest.Name = "CreatePopupRequest"
createPopupRequest.Parent = ReplicatedStorage
Players.CharacterAutoLoads = false

local function onPlayerAdded(player)
	createPopupRequest:InvokeClient(player)
	player:LoadCharacter()
end

Players.PlayerAdded:Connect(onPlayerAdded)


-- ==================================================


-- LocalScript

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local createPopupRequest = ReplicatedStorage:WaitForChild("CreatePopupRequest")

local function onCreatePopupRequested()
	local screen = Instance.new("ScreenGui")
	screen.Parent = playerGui
	local closeButton = Instance.new("TextButton")
	closeButton.Text = "Welcome to the game! Click me to play!"
	closeButton.Size = UDim2.new(0, 300, 0, 50)
	closeButton.Parent = screen

	closeButton.MouseButton1Click:Wait()
	closeButton.Visible = false
end

createPopupRequest.OnClientInvoke = onCreatePopupRequested

I know that this script is copied from the dev hub but I just wanted to learn about remote functions.

Now what I want to do with remote functions is;

  • When a player clicks the purchase button and he got less cash then the amount that the item cost it will say ‘You don’t have enough cash to buy this!’. But if he do got enough cash then this needs to happen ‘This will cost $300 Are you sure?’ and then when the player clicked the purchase button it has to clone.(the cloning progress i already have I just want to know how I can do this with remote functions so no hacker/exploiter can give themself cash.

(@devjden said that if you do everything locally exploiters can give themself as much as they want. So thats why I want to change this.) But is it ok to dom somethings locally like when a player clicks a button etc? If yes then I probably need to change that too :frowning:.

Please help me with this!

Are you sure you’re doing this properly? There’s no way the script has that error in that line unless you copied the entire thing into a single script.

I copied the entire script into one single script. I assume that that is not supposed to be? Can you tell me where the codes needs to be in then?

It’s commented into the script itself, one of them is supposed to be a normal script and the other one is supposed to be a local script.

I know but where do they need to be in in serverscript service?

Ah. This means “Players” is undefined. Change player from

Players.LocalPlayer 

to

game.Players.LocalPlayer

That wouldn’t work either way because of this.

I just realized why it isn’t working. You cannot get localplayer in a server script. One script should be in a regular script in serverscriptservice, and every line of code underneath the “-- LocalScript” must be in a localscript inside starter player scripts

1 Like

Ik I just realized where this was located lol

I already put it in 2 scripts one in a normal script and one in a local script.

The local script MUST be in starterplayerscripts, and the regular script MUST be in serverscriptservice

1 Like

Didnt saw ur post before I posted it.

Put the below code in ServerScriptService in a Script

And put this code in StarterGui in a Local Script

Can someone answer this? Cause the error is solved.

Please mark my answer as a solution if it helped ;). Ill answer that in a sec

LOL, I will do that after you answer my question and then if you have that one correct too then i will mark them as solved both!

Opening a gui with a button is fine. Anything that is purely visual is ok. But anything that you dont want players to be able to change (like the amount of money or which pets they have) should be handled by the server. The visuals are for the client side, the actual work should be done server side

2 Likes

Ok, thanks now i will mark you as solved :upside_down_face:

Players is not nil, its defined in the script:
image

The reason is because he is using Players.LocalPlayer on the server. Players.LocalPlayer will be nil on the server.

@henrydanger5472 Like @kndn_v said, the script is not meant to be one server script. Part of it is supposed to be a local script, part of it is supposed to be a server script. It doesn’t matter where you put either as long as they run.

Look at where the comment is in the script, the part that says LocalScript, and take that and everything after it and put it into a LocalScript. Keep the top stuff in a server script. You can put server scripts inside of ServerScriptService, that’s a place they will run, and you can put the LocalScript inside of StarterPlayer.StarterPlayerScripts.

The error means that on line 28 in your script you are doing nil.WaitForChild (or nil:WaitForChild, the : just means thing.Function(thing, arg1, arg2, arg3) instead of thing.Function(arg1, arg2, arg3), so, that’s why the error is the same).

If you look at what is before WaitForChild on line 28, its player. And if you look at what player equals, you will see that player equals Players.LocalPlayer. If you look at the documentation for LocalPlayer, by searching for LocalPlayer on https://developer.roblox.com/, you will see it tells you it will be nil if used on the server:

So, to fix this, you can see that that piece of code must run on the client (so, in a LocalScript, not a Script).

The top part of the script where it says -- Server is where the code that you should put in a Script is and the part where it says -- LocalScript is where the code that you should put in a LocalScript is.

I have already solved this issue. It is undefined when running on the server, as there is no localplayer on the server. This issue is solved already. I informed them on where to move that section of the script