RemoteEvent Issue

Hello!

I am currently having issues with the RemoteEvent sequence below:

This ServerScript fires a client RemoteEvent if index 1 of a player’s tableData is equal to 1.

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 -- Here
		event:FireClient(player)
		warn("Event Fired!")
	end
end)

The function in this LocalScript should then trigger, but doesn’t work.

local event = game.ReplicatedStorage:WaitForChild("RemoveDoor1")

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

What might be causing the issue?

Thanks! :slight_smile:

After taking a quick look at you code I don’t see anything obvious that is causing this.

  1. Are you sure that doorData[1] == 1? You’re seeing “Event Fired!” in the output, right?
  2. Where is the script located (the workspace, replicatedStorage, serverScriptService, etc)?
  3. Where is the localScript located?

Also you might consider adding a print statement inside the localScript event.OnClientEvent() to eliminate the possibility that the code is running but door:Destroy() isn’t doing anything.

The warning is outputted.

The first script is in ServerScriptService, and the other is in StarterGui.

I also tried the print statment, but nothing is outputting.

The code plays as soon as I join the game. Does that play into it?

That’s weird - I actually don’t know what’s going on. The fact that it runs instantly could have something to do with it though, maybe try adding a wait() like this:

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

	wait(1) -- the one second wait is overkill, you probably can lower it if this works.

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

It turned out to just be timing, the wait() worked. Thanks for the help!

1 Like