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

Script won't find leaderstats or MoneyValue and just stops the script?

It is inside a local script, so its not that, it just doesn't find leaderstats or anything.

local collector = workspace.SushiCollector
script.Parent.Touched:Connect(function(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Sushi Collector") then
		local player = game:GetService("Players").LocalPlayer
		local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local MoneyValue = leaderstats:FindFirstChild("Money")
			print(MoneyValue)
			if MoneyValue then	
				MoneyValue.Value = MoneyValue.Value + 5
				script.Parent:Destroy()
			else print("its not working")
			end	
		end		
	end	
end)

2 comments

0

Whats your leaderstats script?

0

this is in ServerScriptService: local DataStoreService = game:GetService("DataStoreService") local Players = game:GetService("Players") local RunService = game:GetService("RunService") local dataStore = DataStoreService:GetDataStore("Players") local default = { SessionLock = false, Cash = 0 } local updateAsync = Enum.DataStoreRequestType.UpdateAsync local function waitForRequestBudget() local

1 answer

Reply to horvathmf: Heres the code in ServerScriptService:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local dataStore = DataStoreService:GetDataStore("Players")
local default = {
	SessionLock = false,
	Cash = 0
}
local updateAsync = Enum.DataStoreRequestType.UpdateAsync

local function waitForRequestBudget()
	local currentBudget = DataStoreService:GetRequestBudgetForRequestType(updateAsync)

	while currentBudget < 1 do
		currentBudget = DataStoreService:GetRequestBudgetForRequestType(updateAsync)
		wait(5)
	end
end

local function setUp(player)
	local name = player.Name
	local userId = player.UserId
	local key = "Player_" .. userId

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"

	local cash = Instance.new("IntValue")
	cash.Name = "Cash"

	local success, data, shouldWait
	repeat
		waitForRequestBudget()
		success = pcall(dataStore.UpdateAsync, dataStore, key, function(oldData)
			oldData = oldData or default
			if oldData.SessionLock then
				--He is still sessionlocked, so just wait
				if os.time() - oldData.SessionLock < 1800 then
					--session is alive
					shouldWait = true
				else
					--session is dead, take over
					oldData.SessionLock = os.time()
					data = oldData
					return data
				end
			else
				oldData.SessionLock = os.time()
				data = oldData
				return data
			end
		end)

		if shouldWait then
			task.wait(5)
			shouldWait = false
		end
	until (success and data) or not Players:FindFirstChild(name)
	if success and data then
		cash.Value = data.Cash

		cash.Parent = leaderstats
		leaderstats.Parent = player
	end
end

local function save(player, dontLeave, dontWait)
	local userId = player.UserId
	local key = "Player_" .. userId

	local leaderstats = player:FindFirstChild("leaderstats")

	if leaderstats then
		local cashValue = leaderstats.Cash.Value
		local success

		repeat
			if not dontWait then
				waitForRequestBudget()
			end
			success = pcall(dataStore.UpdateAsync, dataStore, key, function()
				return {
					SessionLock = dontLeave and os.time() or nil,
					Cash = cashValue
				}
			end)
		until success
	end
end

local function onShutdown()
	if RunService:IsStudio() then
		task.wait(2)
	else
		local finished = Instance.new("BindableEvent")
		local allPlayers = Players:GetPlayers()
		local leftPlayers = #allPlayers

		for _,player in ipairs(allPlayers) do
			coroutine.wrap(function()
				save(player, nil, true)
				leftPlayers -= 1
				if leftPlayers == 0 then
					finished:Fire()
				end
			end)()
		end

		finished.Event:Wait()
	end
end

for _,player in ipairs(Players:GetPlayers()) do
	coroutine.wrap(setUp)(player)
end

Players.PlayerAdded:Connect(setUp)
Players.PlayerRemoving:Connect(save)
game:BindToClose(onShutdown)

while true do
	wait(60)
	for _,player in ipairs(Players:GetPlayers()) do
		coroutine.wrap(save)(player, true)
	end
end

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