FireServer not working

I have it so when a player presses a button it will use a point. Now the player needs two points in order to complete a certain task and I’m trying to have it so if they use one and leave the server. Since they haven’t completed the task they will get that point back when they rejoin. (I plan to do the same for if the player uses two points and leaves just before the task begins).

--------This is in a LocalScript

game.Players.PlayerRemoving:Connect(function(player)
	if moduleScript.oneButtonPressed == true then
		giveBack1:FireServer()
		print("fired to server")
	end
end) --this part works fine

--------This is in a ServerScript

local giveBack1 = game.ReplicatedStorage.giveBack1
giveBack1.OnServerEvent:Connect(function(player)	
	player.leaderstats[currencyName].Value += 1
    print("byebye")
end)

Since I also have a DataStore system. Just above that ServerScript code is this:

game.Players.PlayerRemoving:Connect(function(player)
	local ID = currencyName.."-"..player.UserId
	DataStore:SetAsync(ID, player.leaderstats[currencyName].Value)
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("This game is shutting down")
		end
	end
	wait(4)
end)

Could it be that something is interfering here and not giving the FireServer event enough time to work?

1 Like

Did you define currencyName in a variable?

I did yes

local currencyName = "Points"

That won’t work as it is expecting a number to be used as an index there.

That applies to GetChildren, not instance indexing.

1 Like

This is the entire ServerScript that is in ServerScriptService

local DataStore = game:GetService("DataStoreService"):GetDataStore("PointDataStore") 

local moduleScript = require(game.ReplicatedStorage:WaitForChild("ModuleScript"))

local currencyName = "Points"

game.Players.PlayerAdded:Connect(function(player)
	local pointFolder = Instance.new("Folder", player)
	pointFolder.Name = "leaderstats"	

	local pointAmount = Instance.new("IntValue", pointFolder)
	pointAmount .Name = currencyName

	local ID = currencyName.."-"..player.UserId
	local savedData = nil
	pcall(function()
		savedData = DataStore:GetAsync(ID)
	end)
	if savedData ~= nil then
		pointAmount.Value = savedData
		print("Data loaded")
	else
		-- New player
		pointAmount.Value = 10
		print("New player to the game")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local ID = currencyName.."-"..player.UserId
	DataStore:SetAsync(ID, player.leaderstats[currencyName].Value)
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("This game is shutting down")
		end
	end
	wait(4)
end)

local pointValueChanged = game.ReplicatedStorage.pointValueChanged
pointValueChanged.OnServerEvent:Connect(function(player)
	player.leaderstats[currencyName].Value -= 1
end)

local giveBack1 = game.ReplicatedStorage.giveBack1
giveBack1.OnServerEvent:Connect(function(player)
	print("byebye")
	player.leaderstats[currencyName].Value += 1
end)

local giveBack2 = game.ReplicatedStorage.giveBack2
giveBack2.OnServerEvent:Connect(function(player)
	player.leaderstats[currencyName].Value += 2
end)

No players are in the game when BindToClose is fired.

1 Like
game:BindToClose(function()
	if moduleScript.oneButtonPressed == true then
		print("setting is true")
	end
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("This game is shutting down")
		end
	end
	wait(5)
end)

I tried this and it didn’t work. Probably because the value gets changed within a LocalScript?

Are you changing the value locally? The values need to be sent to the server and the server needs to change them.

1 Like

It works!!! I made remoteEvents that change the moduleScripts value when called

local oneButtonPressedTrue = game.ReplicatedStorage.oneButtonPressedTrue
oneButtonPressedTrue.OnServerEvent:Connect(function()
	print("set to true")
	moduleScript.oneButtonPressed = true	
end)

local oneButtonPressedFalse = game.ReplicatedStorage.oneButtonPressedFalse
oneButtonPressedFalse.OnServerEvent:Connect(function()
	print("set to false")
	moduleScript.oneButtonPressed = false	
end)

and put giving the player +1 point in the PlayerRemoving function.

game.Players.PlayerRemoving:Connect(function(player)
	if moduleScript.oneButtonPressed == true then
		player.leaderstats[currencyName].Value += 1
	end
	local ID = currencyName.."-"..player.UserId
	DataStore:SetAsync(ID, player.leaderstats[currencyName].Value)
end)

Thanks so much!