Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make leaderstats change when a brick is touched?

I have a game in the process of making, and every time at the end of the level you touch a brick and you complete the level. But I also want to make a shop system so I want them to get money every time they touch the brick at the end. My current leaderstats scripts is this.

Any Ideas?

local dsService = game:GetService("DataStoreService")
local ds = dsService:GetDataStore("MoneyStats")

game.Players.PlayerAdded:Connect(function(plr)
 local folder = Instance.new("Folder", plr)
 folder.Name = "leaderstats"
 local currency = Instance.new("IntValue", folder)
 currency.Name = "Money"
 currency.Value = ds:GetAsync(plr.UserId) or 0
 currency.Changed:Connect(function()
  ds:SetAsync(plr.UserId, currency.Value)
 end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
 ds:SetAsync(plr.UserId, plr.leaderstats.Money.Value) 
end)

1 answer

✓ Accepted answer

Is it an obby game? If it is, make sure all checkpoints (the stages) are all named according to their order (number of stage). The first stage must be a SpawnLocation, make sure AllowTeamChangeOnTouch is turned off and Neutral is turned on.

I recommend adding Stage value in the leaderstats too. The DataStore script should be looking like this:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MoneyStats")

local function create(player)
	local userId = player.UserId
	local key = "Player_" .. userId

	local folder = Instance.new("Folder", plr)
	folder.Name = "leaderstats"
	
	local currency = Instance.new("IntValue", folder)
	currency.Name = "Money"
	
	local stages = Instance.new("IntValue", folder)
	stages.Name = "Stage"
	
	local data
	local success, result = pcall(function()
		data = DataStore:GetAsync(userId)
	end)
	
	if success and data then
		currency.Value = data[1] or 0
		stages.Value = data[2] or 1
	else
		print("Error!" .. result)
	end
end

local function save(player)
	local userId = player.UserId
	local key = "Player_" .. userId
	local leaderstats = player:FindFirstChild
	
	local DataToSave = {
		player.leaderstats.Money.Value
		player.leaderstats.Stage.Value
	}
	
	local success, result = pcall(function()
		DataStore:SetAsync(userId, DataToSave)
	end)
	
	if success then
		print("Success!")
	else
		print("Error!")
		warn(result)
	end
end

game.Players.PlayerAdded:Connect(create)
game.Players.PlayerRemoving:Connect(save)
game:BindToClose(function()
	if game:GetService("RunService"):IsStudio() then return end

	for _, plr in pairs(game.Players:GetPlayers) do
		save(plr)
	end
end)

Then, you create another script in ServerScriptService for the checkpoints/stages, name it "CheckpointSystem".

game.Workspace.ChildAdded:Connect(function(char)
	local plr = game.Players:GetPlayerFromCharacter(char)
	if plr ~= nil then
		local leaderstats = plr.leaderstats
		local stage = game.Workspace.StagesFolder:FindFirstChild(leaderstats.Stage.Value)
		if char:FindFirstChild("HumanoidRootPart") then
			char.HumanoidRootPart.CFrame = char.HumanoidRootPart.CFrame + Vector3.new(0,3,0)
			wait()
			char.HumanoidRootPart.CFrame = stage.CFrame + Vector3.new(0,3,0)
		end
	end
end)

And lastly, this code will function when a player touches a checkpoint, so you put this script in ALL CHECKPOINTS! (except for the first checkpoint)

--This script must be in a "Script", not in a "LocalScript"
local Players = game:GetService("Players")
local part = script.Parent

local function onTouched(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("HumanoidRootPart") and Players:GetPlayerFromCharacter(hit.Parent) then
		local plr = Players:GetPlayerFromCharacter(hit.Parent)
		
		local ls = plr:FindFirstChild("leaderstats")
		
		if ls.Stage.Value < tonumber(part.Name) and ls.Stage.Value == ((tonumber(bricklol.Name)) - 1) then
			ls.Stage.Value = tonumber(bricklol.Name)
			ls.Money.Value = ls.Money.Value + 100
		end
	end
end

part.Touched:Connect(onTouched)

Hope this helps! This took me hours to type this myself and let me know if there are errors or simple mistakes I did. Have a good day! ^^

3 comments

0

Oops I forgot to make the money system in the checkpoints script

0

In line 12, make another line by pressing Enter, then type "ls.Money.Value = ls.Money.Value + 100"

0

Ok I edited it

This discussion is preserved as it was. New answers, comments, and votes are closed.