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

Why my collector script with datastore doesn't work ?

Hello everyone, I made a tycoon that records user data all the time. And as soon as a block of the dropper falls into the collector it makes me win 1 cash so I did that but it doesn't work, why ?

Database script:

local DataStore2 = require(1936396537)
DataStore2.Combine("MasterKey", "Level", "XP", "Cash")
local defaultLevel = 0
local defaultXP = 0
local defaultCash = 0

local XpToLevelUp = function(level)
	return 100 + level * 5
end

game.Players.PlayerAdded:Connect(function(player)
	--Leaderstats
	local leaderstats = Instance.new("Folder", player); leaderstats.Name = "leaderstats"
	
	local levelValue = Instance.new("IntValue", leaderstats); levelValue.Name = "Level"
	local XpValue = Instance.new("IntValue", leaderstats); XpValue.Name = "XP"
	local CashValue = Instance.new("IntValue", leaderstats); CashValue.Name = "Cash"
	
	--Get DataStore
	local levelStore = DataStore2("Level", player)
	local XpStore = DataStore2("XP", player)
	local CashStore = DataStore2("Cash", player)
	
	--Fonction quand le niveau et l'xp et le cash va être mise à jour
	local function updateLevel(level)
		player.leaderstats.Level.Value = level
	end
	
	local function updateCash(cash)
		player.leaderstats.Cash.Value = cash
	end
	
	local function updateXP(xp)
		if xp >= XpToLevelUp(levelStore:Get(defaultLevel)) then
			XpStore:Increment(XpToLevelUp(levelStore:Get(defaultLevel)) * -1)
			levelStore:Increment(1)
		else
			player.leaderstats.XP.Value = xp
		end
	end
	updateLevel(levelStore:Get(defaultLevel))
	updateXP(XpStore:Get(defaultXP))
	updateCash(CashStore:Get(defaultCash))
	
	levelStore:OnUpdate(updateLevel)
	XpStore:OnUpdate(updateXP)
	CashStore:OnUpdate(updateCash)
end)

My collector script

local DataStore2 = require(1936396537)
local collector= game.Workspace.House2.Purchases.Conveyer.collector
local leaderstats = game.Players.Player.leaderstats.Cash
local function onTouchEnded(part)
	for k, player in pairs(game.Players:GetChildren()) do
		local xpStore = DataStore2("Cash", player)
		xpStore:Increment(1)
		print("Touch ended: " .. part.Name)
		part:Destroy()
	end
end

while true do
	wait(0.5)
	recolteur.Touched:Connect(onTouchEnded)		
end

2 comments

1

don't put events in loops!

0

ah i see you also use DataStore2... im not alone :D

1 answer

✓ Accepted answer

I might have spotted your problem. First of all, the recolteur.Touched:Connect(onTouchEnded) event only needs to be connected once. Instead of

while true do
	wait(0.5)
	recolteur.Touched:Connect(onTouchEnded)
end

you only need

recolteur.Touched:Connect(onTouchEnded)

(yes it's that simple)

but that's just an optimization thing I'm pretty sure. I noticed that you do

local xpStore = DataStore2("Cash", player)
xpStore:Increment(1)

I think that you simply reference the wrong datastore. If that's not it, are there any errors generated? Does it even print "Touch Ended"? If not, just print debug.

2 comments

1

It's not just an optimization thing, repeatedly connecting it means that the function onTouchEnded will run multiple times every time something touched it.

0

yeah

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