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
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.
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
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.