Problem with if statement

Hi. I want “if player.Backpack:FindFirstChild(“Paper”) == nil then” line to work more than once.

The line works perfectly when it’s executed the first time, because the backpack doesn’t contain “Paper”. However, the second time, after using :Destroy() (and yes it does work correctly in-game), it simply sees the line as false, which I don’t understand why. It’s like “Paper” is still there but invisible. Maybe I should do something other than :Destroy()? Or is it something completely different. Here is all my code:

local tool=nil
local ScreenGui=script.Parent.ScreenGui
local Frame=ScreenGui.Frame
local TextBox=Frame.Text
local Submit=Frame.btn_Submit
local bool=true
local PaperEvent=game:GetService("ReplicatedStorage").PaperEvent
local gui=script.Parent.ScreenGui
local function onPlayerClicked(hit)
	local player = hit
	if not player then
		return
	end
	if player.Backpack:FindFirstChild("Paper") == nil then --The problematic line
		tool = Instance.new("Tool")
		tool.Name = "Paper"
		tool.ToolTip = "You can write here."
		tool.Parent = player.Backpack

		local handle = Instance.new("Part", tool)
		handle.Name = "Handle"
		handle.Material = "SmoothPlastic"
		handle.Size = Vector3.new(2, 0.05, 3)
		handle.BrickColor = BrickColor.new(242, 243, 243)

		tool.Equipped:Connect(function()
			if bool then
				ScreenGui:Clone().Parent = player.PlayerGui
				bool = false
			end
		end)
		PaperEvent:FireClient(player)
	end
end

script.Parent.ClickDetector.MouseClick:Connect(onPlayerClicked)

:Destroy() script:

local PaperEvent=game:GetService("ReplicatedStorage").PaperEvent
PaperEvent.OnClientEvent:Connect(function()
	local player = game:GetService("Players").LocalPlayer
	local backpack=player:FindFirstChild("Backpack")
	local tool=backpack:FindFirstChild("Paper")
	local part = game:GetService("Workspace").Wall

	part.ClickDetector.MouseClick:Connect(function()
		tool.Handle.CFrame = part.CFrame
		tool.Handle.Parent = part
		tool:Destroy()
	end)
end)

Last script:

local Players=game:GetService("Players")
local player=Players.LocalPlayer
local Frame=script.Parent
local Submit=Frame.btn_Submit
local tool=player.Character:FindFirstChildOfClass("Tool")
Submit.MouseButton1Click:Connect(function()
	Frame.Visible=false
	player.Character.Humanoid:UnequipTools()
	tool.Equipped:Connect(function()
		Frame.Visible=true
	end)
end)

Just remove the == nil. That might fix.

It seems like you create the tool on the server end and try to destroy it on the local end. You can confirm this to be the case by testing out the place (hit Play) and changing the setting “Current: Client” to “Current: Server” on the Home or Test tab of studio. You’ll notice that while viewing the inventory from the client, the tool will be visibly gone from the hierarchy, while it’s still there on the server end. This is because changes to instances on the client side will not affect the mirroring instances on the global side. It’s best practice not to destroy instances locally unless they were created locally.

My suggestion is for you to refactor your code so that the destroy event is handled on the server end.

1 Like

Thank you so much, that seems to be the case.

1 Like

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