Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Selection box not showing after resetting character and parent becomes nil?


local delete = game:GetService("ReplicatedStorage"):WaitForChild("Delete")
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()

local selectionBox = Instance.new("SelectionBox")
selectionBox.Color3 = Color3.new(0, 1, 0)
selectionBox.LineThickness = 0.01
selectionBox.Parent = player.PlayerGui

local debounce = false
local isEquipped
local target

local function onActivated ()
	if debounce == false then
		if target and target.Name == "Deletable" and player:DistanceFromCharacter(target.Position) < 18 then
			debounce = true
			delete:FireServer(target)
			target:Destroy()
			selectionBox.Color3 = Color3.new(1, 0, 0)
			target = mouse.Target
			if target and target.Name == "Deletable" and player:DistanceFromCharacter(target.Position) < 18 then
				selectionBox.Adornee = target
			else
				selectionBox.Adornee = nil
			end
			wait(0.2)
			selectionBox.Color3 = Color3.new(0, 1, 0)
			debounce = false
		end
	end
end

local function onEquipped ()
	print("Equipped!")
	isEquipped = true
	while isEquipped do
		target = mouse.Target
		print("Checking!")
		if target and target.Name == "Deletable" and player:DistanceFromCharacter(target.Position) < 18 then
			print("Setting adornee!")
			selectionBox.Adornee = target
			print(selectionBox.Parent.Name)
		else
			selectionBox.Adornee = nil
		end
		wait(0.1)
	end
end

local function onUnequipped ()
	isEquipped = false
	selectionBox.Adornee = nil
end

script.Parent.Activated:Connect(onActivated)
script.Parent.Equipped:Connect(onEquipped)
script.Parent.Unequipped:Connect(onUnequipped)

This is a local script placed in a tool in the starterpack. The script works normally with expected results at first (Parent is PlayerGui), But after resetting the character, the parent of the selectionBox becomes nil.

Any ideas why this is happening?

1 comment

0

Update: It works after setting the parent of selectionBox to workspace, but I want it to be in the player PlayerGui

2 answers

✓ Accepted answer

Roblox automatically deletes all objects in the PlayerGui except ScreenGuis which have their ResetOnSpawn property set to false, when the character respawns

After some thinking, I used this at the start of the script:

local selectionBox = Instance.new("SelectionBox")
selectionBox.Color3 = Color3.new(0, 1, 0)
selectionBox.LineThickness = 0.01
selectionBox.Parent = player.PlayerGui

print(selectionBox.Parent.Name)
wait(5)
print(selectionBox.Parent.Name)

And surprisingly, after resetting the character I got this output:

22:24:46.897  PlayerGui  -  Client - Delete:11
22:24:51.910  Players.CjayPlyz.Backpack.Delete.Delete:13: attempt to index nil with 'Name'  -  Client - Delete:13

So it looks likes that the selectionBox is getting deleted or maybe overwritten. Well, the script works perfectly now if I wait 5 seconds before making the selectionBox

This discussion is preserved as it was. New answers, comments, and votes are closed.