Modifying a Table of Arrays

Hello!

The script below should set the first index of a player’s table called door data from 0 to 1 when the “RemoveDoor1” RemoteEvent is fired. However, the script fails to do so. What could be causing the issue? The issue can be found in the last function. The script is able to properly load in and save a table from the DataStores.

local openGui = game.ReplicatedStorage:WaitForChild("OpenDoor1Gui")
local door = game.Workspace:WaitForChild("Zone1Door")
local purchaseGui = game.StarterGui.Door1PurchaseGUI.Frame
local event = game.ReplicatedStorage:WaitForChild("RemoveDoor1")

local DataStoreService = game:GetService("DataStoreService")
local DoorsUnlocked = DataStoreService:GetDataStore("DoorsUnlocked")
local DoorData = {}

game.Players.PlayerAdded:Connect(function(player)

	DoorData[player] = DoorsUnlocked:GetAsync(player.UserId)
	warn("Door Data Loaded!")
	print(DoorData[player])
	if not DoorData[player] then
		DoorData[player] = {0, 0}
		DoorsUnlocked:SetAsync(player.UserId, DoorData[player])
		warn("New Data Created!")
	end
	
	if DoorData[1] == 1 then
		event:FireClient(player)
		warn("Event Fired!")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	DoorsUnlocked:SetAsync(player.UserId, DoorData[player])
	DoorData[player] = nil
	warn("Door Data Saved!")
end)

door.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if not player or not player:IsA("Player") then return end

	local tokens = player.leaderstats.Tokens
	local debounce = false
	if tokens.Value >= 3000 and debounce == false then
		debounce = true
		openGui:FireClient(player)
		task.wait(0.5)
		debounce = false
	end
end)

event.OnServerEvent:Connect(function(player)
	local tokens = player.leaderstats.Tokens
	tokens.Value -= 3000
	DoorData[1] = 1
	print(DoorData[player])
end)

Thanks! :slight_smile:

(line 50)

I believe you intended to do

DoorData[player][1]

That works, thanks!

This is an unrelated issue, but there now is a problem with the script segment below. Did I do something wrong with the RemoteEvent? The RemoteEvent triggers a function in a LocalScript within StarterGui.

	if DoorData[player][1] == 1 then
		event:FireClient(player)
		warn("Event Fired!")
	end

Does Event Fired get outputted?
What is the script on the client side?

also, did you adjust DoorData[1]==1 in all places that it occured,or just in one place?

The warning does get outputted and I adjusted all of those lines. It most likely is a script/client issue.

There is another RemoteEvent, openGui, that opens a ScreenGui. If the player clicks a “Yes” button in the Gui, the “event” RemoteEvent fires.

Script, FireClient → LocalScript, FireServer → LocalScript, Function

vs.

Script, FireClient → LocalScript, Function

Here are the scripts:

local player = game.Players.LocalPlayer
local tokens = player.leaderstats.Tokens.Value
local openGui = game.ReplicatedStorage:WaitForChild("OpenDoor1Gui")
local removeDoor = game.ReplicatedStorage:WaitForChild("RemoveDoor1")
local purchaseGui = script.Parent
local yesButton = script.Parent.Yes
local noButton = script.Parent.No

openGui.OnClientEvent:Connect(function()
	purchaseGui.Visible = true
end)

yesButton.Activated:Connect(function()
	local door = workspace.Zone1Door
	door:Destroy()
	purchaseGui.Visible = false
	removeDoor:FireServer()
end)

noButton.Activated:Connect(function()
	purchaseGui.Visible = false
end)
local event = game.ReplicatedStorage:WaitForChild("RemoveDoor1")

event.OnClientEvent:Connect(function()
	local door = workspace.Zone1Door
	door:Destroy()
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.