I don’t know if it is possible to do this, but I wanted to make it to where if someone bought a house, and then left the game and rejoined, the house would still be owned by them. Is this possible? and if so, this is the script I have now
local buypart = script.Parent.Parent.BuyPart
local labelpart = script.Parent.Parent.LabelPart
owned = false
price = 200
open = false
buypart.ClickDetector.MouseClick:Connect(function(player)
local cash = player.leaderstats.Cash
if cash.Value >= price and owned == false then
labelpart.BillboardGui.TextLabel.Text = player.Name.."'s House"
cash.Value = cash.Value - price
owned = true
else
print(player.Name.." cannot afford house!")
end
end)
In the case of saving things, you would use a DataStore.
DataStores are a relatively simple thing. They save data, and then retrieve it back.
When it comes to saving the house, I would do something along the lines of
local ownHouse1 = false
local function getDataFromDataStore(player)
-- Insert code here
end
if getDataFromDataStore(player)[ownHouse1] then
ownHouse1 = true
end
Very janky code, and not something I’d recommend. But for starters, this works. Make sure you read the documentation (or alternatively look up a youtube tutorial) on how to properly use DataStores.
use datastore and add boolvalue inside player and set bool to true once player bought a house . or even string 0 = doesnt have , 1 = have
-- I dont recommend to use normaldatastore , profileservicebetter
local DataStoreService = game:GetService("DataStoreService") --Gets the DataStoreService
local dataStore = DataStoreService:GetDataStore("Test") --Name of your datastore
--//Function
local function saveData(player) --Create out own function for saving data
local tableToSave = { --Creates a table for our values
player.HouseOwned.Value
player.leaderstats.Money.Value
--You can continue the list in the same order by adding "player.leaderstats.IntValueName.Value;"
}
local success, errorMessage = pcall(dataStore.SetAsync, dataStore, player.UserId, tableToSave) --Checks if datastore succeeded
if success then --If datastore success then
print("Data has been saved!")
else
print("Data has not been saved!")
end
end
game.Players.PlayerAdded:Connect(function(player) --When a player joins the game
player.CharacterAdded:Wait() --Wait for the player to load
local leaderstats = Instance.new("Folder") --Creates a new folder for the player
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local points = Instance.new("IntValue") --Creates an IntValue
points.Name = "Money"
points.Parent = leaderstats
points.Value = 0 -- this is for starter only and if data is nil
local Bool = Instance.new("StringValue")
Bool.Name = "HouseOwned"
Bool.Parent = player
Bool.Value = 0 -- doesnt have
local data = nil --Data is empty
local success, errorMessage = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
if success and data then --If player does have data
points.Value = data[1]
Bool.Value = data[2] -
else
print("The Player has no Data!")
warn(errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
saveData(player)
end)
game:BindToClose(function() --When the game's servers are shutting down
for _, player in ipairs(game.Players:GetPlayers()) do
task.spawn(saveData, player) --save all the player's data
end
end)```