game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Credits = Instance.new("IntValue", leaderstats)
Credits.Name = "Credits"
local Research = Instance.new("IntValue", leaderstats)
Research.Name = "Research"
--Load Datalocal data
local success , err = pcall(function()
data = DataStore1:GetAsync(player.UserId)
end)
if success and data ~= nil and success then
Credits.Value = data[1]
Research.Value = data[2]
elseif data == nil and success then
Credits.Value = 500
Research.Value = 10
end
end)
game.ReplicatedStorage:WaitForChild("CheckPrice").OnServerInvoke = function(player,NameOfSpaceVehicle) --- The script.script.Name
return game.ServerStorage.SpaceVehicles:FindFirstChild(NameOfSpaceVehicle).Price.Value ---Price located in the vehicle
end
game.ReplicatedStorage:WaitForChild("SpawnSpaceVehicle").OnServerInvoke = function(player,NameOfSpaceVehicle)
local Credits = game.player.leaderstats["Credits"].Value
local Research = game.player.leaderstats["Research"].Value
local PlayerUserId = "Player_"..player.UserId
local SpaceVehicle = game.ServerStorage.SpaceVehicles:FindFirstChild(NameOfSpaceVehicle):Clone()
SpaceVehicle:SetPrimaryCFrame(Vector3.new(-183.5,1,-126.5))
SpaceVehicle.Parent=workspace
SpaceVehicle:MakeJoints()
SpaceVehicle.Name = player.UserId.."'s "+NameOfSpaceVehicle
end
game.Players.PlayerRemoving:Connect(function(player)
local PlayerUserId = "Player_"..player.UserId
local data = {}
for _, v in ipairs(player.leaderstats:GetChildren())do
table.insert(data, v.Value)
end
local success, errormessage = pcall(function()
DataStore1:SetAsync(PlayerUserId,data)
end)
if success then
print("Data Successfully Saved")
else
print("There was an error saving data")
warn(errormessage)
end
end)```
The GUI Script
```script.Parent.MouseButton1Click:Connect(function()
local Price=game.ReplicatedStorage:WaitForChild("Check Price"):InvokeServer("script.Parent.Parent.Name")
if game.Players.LocalPlayer.leaderstats.Credits.Value >= Price then --enough credits
game.Players.LocalPlayer.leaderstats.Credits.Value=game.Players.LocalPlayer.leaderstats.Credits.Value - Price ---deduction
game.ReplicatedStorage.SpawnSpaceVehicle:FireServer(script.Parent.Parent.Name)
end
end)```
is the output error "attempt to index nil with leaderstats " from this code?
You shouldn’t keep your scripts in workspace, that’s what ServerScriptService is for, also don’t alter stats on the Client, i.e through a Local Script, do it on the Server so you can save those values.
It gives me that error, but the code still runs, I don’t really worry about it since I will change that code later. Also I noticed the code you sent me the data is highlighted and is unknown(should I make it local), and a new error: OnServerInvoke is not a valid member of RemoteEvent