Button contents not saving and automatically buying

Hello,
So I’m trying to make a tycoon game with a button that automatically saves. The button itself works, just the saving is not. I don’t know what the issue is and tried numerous solutions, but nothing seems to work.

Here is the general topic/area of my script:

wait(1.5)
local button = script.Parent
local savedParent = button.Parent.Parent
local debounce = 1
local isDebounce = false
local p = nil
local bought = false

button.Tycoon.Value.Parent.Claimed.Event:Connect(function(plr:Player)
    print('event fired')
	p = plr
	while true do
		local success, result = pcall(function()
			return game:GetService('DataStoreService'):GetDataStore('UserOwnsButton'):GetAsync(tostring(plr.UserId)..'_'..button.Parent.Name)
		end)
		if success then
			if result then
				-- buy stuff
			else
				bought = false
			end
			break
		else
			warn('Error getting DataStore')
			bought = false
		end
		wait(math.random(0.5, 1.5))
	end
end)

button.Touched:Connect(function(hit)
	local owner = button.Tycoon.Value.Parent:FindFirstChild('Owner').Value
	if hit and hit.Parent and game.Players:GetPlayerFromCharacter(hit.Parent) and (game.Players:GetPlayerFromCharacter(hit.Parent) == p or (button.Tycoon.Value.Parent:FindFirstChild('Owner') and game.Players:GetPlayerFromCharacter(hit.Parent) == button.Tycoon.Value.Parent:FindFirstChild('Owner').Value)) and not isDebounce then
		isDebounce = true
		local ls = owner:FindFirstChild('leaderstats')
		if ls.Money.Value >= button.Cost.Value then
			local success, result = pcall(function()
				return game:GetService('DataStoreService'):GetDataStore('UserOwnsButton'):SetAsync(tostring(owner.UserId)..'_'..button.Parent.Name, true)
			end)
			if not success then
				warn('Error setting DataStore')
				task.wait(debounce)
				isDebounce = false
				return
            else
            print('saved')
			end
			-- more buying stuff
			ls.Money.Value -= button.Cost.Value
		end
		wait(debounce)
		isDebounce = false
	end
end)

Notes:

  • button.Tycoon.Value.Parent.Claimed is a BindableEvent
    • the print('event fired') line does not fire at all
    • I looked at other posts about this and it said to add a wait() function for the script to connect to the BindableEvent
  • Buying stuff for works and the print function claims that the button saved

Any ideas for what I may be doing wrong?

1 Like