Script Not Giving Clean when Option is Enabled

Hey there!

I’m about to start a show with my cousin in a WWE game, and I need to have it finished before the show starts. I am working on a script where if I have it where you can get Cleans automatically, it will give it to the player, and remove it from the player when it’s disabled.

I just tried testing it and checked if there was any issues that was in my code, and I can’t seem to find anything that could make it not work.

Could anyone help me debug it so it can work?
Here is the script inside the server, since it does work Locally by using print(data).

--// Variables
local Players = game:GetService("Players")
local Remotes = game:GetService("ReplicatedStorage").Remotes
local CleansAllowedCheck = Remotes.CleansAllowed

local Gears = game:GetService("ReplicatedStorage").Gears
local Clean = Gears.Clean:Clone()

--// Script
CleansAllowedCheck.OnServerEvent:Connect(function(player, data)
	print(data)
	if data == "Allowed" then
		player.CharacterAdded:Connect(function(character)
			local backpack = player:FindFirstChild("Backpack")
			local CleanFound = backpack:FindFirstChild("Clean")
			
			if not CleanFound then
				Clean.Parent = backpack
			end
		end)
	elseif data == "Denied" then
		player.CharacterAdded:Connect(function(character)
			local backpack = player:FindFirstChild("Backpack")
			local CleanFound = backpack:FindFirstChild("Clean")
			
			if CleanFound then
				CleanFound:Destroy()
			elseif not CleanFound then
				print(`Player does not have a clean, so I can't remove anything!`)
			end
		end)
	end
end)

all players share the same Clean (cloned) ?

That could be the cause… I’ll make it where if the player joins they will receive their own copy.

Made it so every player has their own copy, but this issue still stands…

The player’s character may have loaded in by the time the it gets to the player.CharacterAdded function, therefore the function doesn’t run. Try checking if the character has already loaded in.

local function giveClean(player)
	local Backpack = player:FindFirstChild("Backpack")

	if Backpack:FindFirstChild("Clean") then
		Clean:Clone().Parent = Backpack
	end
end

if data == "Allowed" then
	if player.Character then
		giveClean(player)
	end

	player.CharacterAdded:Connect(function()
		giveClean(player)
	end
end

I’ve just tried this code, but my issue is still there. What other thing am I missing?

What is the result when you use print(data)? If it prints "Allowed", show us the code for the local script firing the remote event. The server seem fine, so it might be a problem on the client end.

It does indeed print “Allow”. I’m using the TopbarPlus V3 script so I had to use :bindEvent to actually make the button do something.

local GameSettings = Icon.new()
	
	GameSettings:modifyTheme({"Menu", "MaxIcons", 3})
	GameSettings:align("Left")
	GameSettings:setImage(6022852108)
	GameSettings:setImageScale(0.65)
	GameSettings:setCaption("Game Settings")
	GameSettings:setMenu({
		Icon.new()
			:autoDeselect(false)
			:setLabel("Roof: On", "deselected")
			:bindEvent("deselected", function()
				game.ReplicatedStorage.Remotes.RoofEnabled:FireServer(0)
			end)
			:setLabel("Roof Off", "selected")
			:bindEvent("selected", function()
				game.ReplicatedStorage.Remotes.RoofEnabled:FireServer(1)
			end)
		,
		Icon.new()
			:autoDeselect(false)
			:setLabel("Daytime: Off", "deselected")
			:bindEvent("deselected", function()
				game.ReplicatedStorage.Remotes.Daytime:FireServer(0)
			end)
			:setLabel("Daytime: On", "selected")
			:bindEvent("selected", function()
				game.ReplicatedStorage.Remotes.Daytime:FireServer(11)
			end)
		,
		Icon.new()
			:autoDeselect(false)
			:setLabel("Cleans: On", "deselected")
			:bindEvent("deselected", function()
				game.ReplicatedStorage.Remotes.CleansAllowed:FireServer("Allowed")
			end)
			:setLabel("Cleans: Off", "selected")
			:bindEvent("selected", function()
				game.ReplicatedStorage.Remotes.CleansAllowed:FireServer("Denied")
			end)
		,
	})

Edit 1: The code that fires the server at the bottom of the script.

Everything looks fine here, so I think we need to do print debugging. Try putting print statements after two of these lines, and tell me if they print:

  1. if data == "Allowed" then
  2. if player.Character then