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

How to fix: attempt to index nil with 'Event'?

So I've made a datastore script to save the player's data. I keep getting this error when the last player leaves: ServerScriptService.DataStore:54: attempt to index nil with 'Event'

This is the script:


local DataStoreService = game:GetService("DataStoreService")

local playerDataStore = DataStoreService:GetDataStore("playerDataStore")

local playersLeft = 0

game.Players.PlayerAdded:Connect(function(player)
	
	playersLeft = playersLeft + 1
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local tix = Instance.new("IntValue")
	tix.Name = "Tix"
	tix.Parent = leaderstats
	
	local data
	local success, errormessage = pcall (function()
		data = playerDataStore:GetAsync(player.UserId.."-tix")
	end)
	
	if success then
		tix.Value = data
		print("Player data loaded successfuly!")
	else
		print("There was an error whilst getting your data!")
		warn(errormessage)
	end
end)

local bindableEvent

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage pcall(function()
		playerDataStore:SetAsync(player.UserId.."-tix",player.leaderstats.Tix.Value)
	end)
	
	if success then
		print("Player data successfuly saved!")
		playersLeft = playersLeft - 1
		bindableEvent:Fire()
	else
		print("There was an error when saving data!")
		warn(errormessage)
	end
	
end)

game:BindToClose(function()
	while playersLeft > 0 do
		bindableEvent.Event:Wait()
	end
end)

Anyone knows what is wrong and can help me?

1 comment

1

You did not properly define bindableEvent on line 33

1 answer

✓ Accepted answer

the problem is that bindableEvent is nil at line 33

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