If you don't know what a simulator game is, they're basically games where most of the time, you have a tool that gives you something. You can then sell that thing for money, which you use to buy upgrades to give you more things that give you things that give you money. They're also just pay 2 win money traps....
Today, we will learn about DataStores, and leaderstats, the first part of simulators. We will also make a simple part that sells your value, and make a tool that gives you a value.
Requirements:
Difficulty:
First of all, make a script in ServerScriptService, and call it leaderstats. Inside, put this:
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("folder") -- our folder
leaderstats.Parent = player
leaderstats.Name = "leaderstats" -- very important, dont change name
local strength = Instance.new("IntValue") -- name it whatever
strength.Parent = leaderstats
strength.Name = "Strength" -- name it whatever again
strength.Value = 0 -- this is the player's starting value
local cash = Instance.new("IntValue") -- name it whatever
cash.Parent = leaderstats
cash.Name = Cash -- name it whatever again
cash.Value = 0 -- this is the player's starting value
end)
Now, if you test your game, you should see the values in your name.
So, depending on what your simulator is about, you will want a different tool. For me, I did "strength," so I would probably want to use a weight. Just go in the toolbox and get a weight, or whatever. You can also make your own model and make it a tool, but I am not going to be showing how to do that. First, make a script inside of Workspace, name it MainEvent. Do not put anything in the script. Now, put a RemoteEvent inside of it, and call it ToolEvent. Now, inside the tool you chose, put a LocalScript inside of it. In the localscript, put this
local tool = script.Parent -- the tool
tool.Activated:Connect(function() -- player clicked tool event
workspace.MainEvent.ToolEvent:FireServer() -- sends message to server
end)
Inside of MainEvent, put this:
local remoteevent = script.ToolEvent
local player = game.Players.LocalPlayer
remoteevent.OnServerEvent:Connect(function() -- recieves message from server
player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 1 -- or whatever value
end)
Now, your tool should work.
Make a part in Workspace, then edit it to however you want. Name it SellPart, or something with "Sell" in it, to get less confusing. Now, put a Script inside of it:
local part = script.Parent
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local cash = player.leaderstats.Cash
local selling = player.leaderstats.Strength
cash.Value = cash.Value + selling.Value * 1
selling.Value = 0
end
end)
Step 1: Publish your game Step 2: Enable API Access in Settings Step 3: Put this script in ServerScriptService:
local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("PlayerData")
game.Players.PlayerAdded:Connect(function(player)
local data = ds:GetAsyc(player.UserId)
local strength = player.leaderstats.Strength
local cash = player.leaderstats.Cash
strength.Value = data[1]
cash.Value = data[2]
end)
game.Players.PlayerRemoving:Connect(function(player)
local data = {
strength = player.leaderstats.Strength.Value,
cash = player.leaderstats.Cash.Value,
}
ds:SetAsync(player.UserId,data)
end)
And that is how to make the first parts of a simulator. See you next time!