Script gives error when making variable

Hello,

I’m working with remote functions lately, but now when the script wants to check if the player has enough cash it gives an error cause its no longer in a local script. And I have no idea how I can make it that it works just fine in a normal script in serverscriptservice.

Can someone help me with this?

Here’s the serverscript;

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local createPartRequest = Instance.new("RemoteFunction")
createPartRequest.Parent = ReplicatedStorage
createPartRequest.Name = "CreatePartRequest"

local player = game.Players.LocalPlayer
local Cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
local leaderstats = player.leaderstats

local function onCreatePartRequested(player)
	
	local tvclone = game.ReplicatedStorage.Tvs.OldTv.PartsOfOldTv:Clone()
	tvclone.Parent = game.Workspace

	for i, v in pairs(tvclone:GetChildren()) do
		v.Anchored = false
		v.CanCollide = true
		v.Transparency = 0
	end

	
	if Cash.Value >= 300 then
		return tvclone
	else
		print("You don't have enough cash to buy this!")
		
	end
end

createPartRequest.OnServerInvoke = onCreatePartRequested
2 Likes

Can you show us the error you’re getting?

image

The error comes from ServerScriptService, I assume it’s a server-script.
LocalPlayer only exists on the client, not the server.

local player = game.Players.LocalPlayer

EDIT - It seems like the leaderstats have not been created when the script tries to access it.
Make sure it exists when the server invokes the client instead.

Yea I know but do you have any idea how I can make it work on a normal server?

I would recommend a RemoteEvent instead.
If you want the script to affect the player activating it, it has to be a localscript.

OnServerInvoke event does contain a player argument itself. You don’t need to define it again.

@AlphazxParticles

This is not correct. OnServerInvoke is a function itself. You don’t need to use Connect again.

No, remote functions are going to return stuff remote event not so thats why I am using remote functions!

At least fix your code, LocalPlayer does NOT exist on the server-side.
You already have the player parameter in your onCreatePartRequested function.

Does anyone else know how I can make it work?

Yes, by removing your player variable above your function.
Try that in the first place.

Just delete the line you are defining with LocalPlayer and move the lines which you defined something with the player you wrote out of the invoke, then you will see it will be working.

This is your revised script.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local createPartRequest = Instance.new("RemoteFunction")
createPartRequest.Parent = ReplicatedStorage
createPartRequest.Name = "CreatePartRequest"

local function onCreatePartRequested(player)

    local tvclone = game.ReplicatedStorage.Tvs.OldTv.PartsOfOldTv:Clone()
    tvclone.Parent = game.Workspace

    local Cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
    local leaderstats = player.leaderstats

    for i, v in pairs(tvclone:GetChildren()) do
	    v.Anchored = false
	    v.CanCollide = true
	    v.Transparency = 0
    end


    if Cash.Value >= 300 then
	    return tvclone
    else
	    print("You don't have enough cash to buy this!")	
    end
end

createPartRequest.OnServerInvoke = onCreatePartRequested

I did that but that doesnt works…

At least elaborate a little bit more than “it doesn’t work”, what’s the error?

image

This doesn’t works either cause when I change the value to 200 then it will clone either way.

Yes, this is because you are cloning it before checking the value. Simply do

if Cash.Value >= 300 then
    local tvclone = game.ReplicatedStorage.Tvs.OldTv.PartsOfOldTv:Clone()
    tvclone.Parent = game.Workspace
else
    print("You don't have enough cash to buy this!")	
end

Delete the first two line at the beginning of the function and move them into the if statement.

Even then it still says the same error as above.

its still saying this error ServerScriptService.TvScripts.Philips:21 attempt to index nil with ‘value’

When is the leaderstats & Cash object created? It seems like it can’t find it.