Really strange tool issues

  1. What do you want to achieve?

I want to make a stylized hotbar, with the ability to equip and unequip a light source.

  1. What is the issue?

Whenever I die after the first time of having the whole gui enabled, and then try to equip the candle, this is what is sent in the output:

The Parent property of Candle is locked, current parent: NULL, new parent: (my character)

Here are the scripts that I’ve written for the system:
Name: EquipLight


Name: EquippedLight

Name: SpawnReset
image
Name: ReinvigorateScript
image
The actual ScreenGui Object
image
Inside the Background ImageLabel:
image

CanContinue is basically a wall preventing some scripts to continue before the “pick up” proximity prompt is triggered.

Also, for some reason, when I disable SpawnReset, when I equip the candle, part of it is just gone (there’s also no NULL parent output message):

First time equipping the candle after “picking it up” (enabling everything):

After dying:

  1. What solutions have you tried so far?

The SpawnReset script has had me try cloning the previous candle, and destroying the previous candle, making the previous the now current candle when you die (cycle repeats). I’ve also tried parenting it to StarterGear which lead to no avail, only sending the same message in the output. (without the SpawnReset script) I’ve tried tracking down the wax part of the candle, but it’s just absolutely gone. Whenever I try to teleport to it or above it, I just end up in the void.

1 Like

I looked a little bit into the issue, and apparently that (the quote above) is exactly what causes your error. Here’s an explanation on how it works:

Random fun fact: You get this warning if you undo the deletion of an instance in studio.

2 Likes

The backpack gets wiped upon death of the character, so your cloning something which parents has been set to nil

2 Likes

Okay, sounds good. I’ll keep that disabled. What would you say the whole problem with the wax and the particle getting deleted is?

Looking at it, I assume that the script doesn’t update the viewportframe’s camera on death/respawn but frankly I’m not sure.

Something else I do think is more plausible though, is that your UI may reset on death, which may screw with your code.

image

It’s not the viewportframe that’s the problem, it’s the actual tool object that’s the problem. The wax in the actual tool is gone. Sorry should have specified that

That’s okay! Sorry for the late response, and thanks for clearing that up. I think it may be some sort of mismatch of the ViewportFrame with the Candle model in the backpack being deleted, cloned and reparented. Could I see how the candle looks like in the backpack (in the explorer) in-game, please? I’m not sure how much of it is in the viewport and how much is in the physical game.

Edit: I’m pretty sure it doesn’t have to do with any UI properties. I set up a really simple UI and reset; The viewport frame (and it’s children) were still there (UI’s ResetOnSpawn property was set to true). I may be wrong though.

Ok, will send some screenshots (the viewportframe also doesn’t use the candle, it uses a model of it):

Hasn’t died yet:


image
Has died and had it equipped while dying:

Has died and didn’t have it equipped while dying:

image

Sorry, only realised now that for some reason it depends if i had it equipped while dying or not.

also sorry for my digital art handwriting

There is no need to clone and destroy flashlight while in the backpack.

You can put the flashlight tool into the ServerStorage, and in server side clone it and put it in the player backpack each time the character spawn.

In client side, you only have to move the flashlight parent from backpack to character to equip and character to backpack to unequip, without cloning and destroying it at all, only changing parent.

Server Side

local PlayerService = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

local Flashlight = ServerStorage:WaitForChild("Flashlight")

local function SetupFlashlight(NewPlayer)
    local Backpack = NewPlayer:WaitForChild("Backpack")

    if Backpack then
        Flashlight:Clone().Parent = Backpack
    end
end

PlayerService.PlayerAdded:Connect(function(NewPlayer)
    NewPlayer.CharacterAdded:Connect(function()
        SetupFlashlight(NewPlayer)
    end)
end)

Client Side

local PlayerService = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

local Player = PlayerService.LocalPlayer
local Character = Player.Character
local Backpack = Player:WaitForChild("Backpack")
local Flashlight = Backpack and Backpack:WaitForChild("Flashlight")

local function UpdateBackpack()
    Character = Player.Character
    Backpack = Player:WaitForChild("Backpack")
    Flashlight = Backpack and Backpack:WaitForChild("Flashlight")
end

local function SetupFlashlight()
    if Backpack and Flashlight and Character and Flashlight.Parent == Backpack then
        Flashlight.Parent = Character
    elseif Backpack and Flashlight and Character and Flashlight.Parent == Character then
        Flashlight.Parent = Backpack
    end
end

Player.CharacterAdded:Connect(UpdateBackpack)
UserInputService.InputBegan:Connect(function(Input, Processed)
    if Input.KeyCode == Enum.KeyCode.F and not Processed then
        SetupFlashlight()
    end
end)
2 Likes

Don’t worry!
I just remembered that, when you equip a tool, it’s Parent becomes the Player rather than the Backpack. What happens in your code is that your SpawnReset script defines the flashlight variable as an existing flashlight model within the Backpack, where it then tries to clone it after you respawn. The issue is that everything within a backpack is cleared upon death (and your code is trying to clone something that’s already cleared), so you’d either want to Clone the instance before the user dies and keep such clone as a reference (only to clone it again when it should be used), or follow @Crygen54’s advice.

The reason your error appeared, if I’m not mistaken, is because your code was trying to reference an Instance that had already been destroyed after death (and it’s Parent therefore being “NULL”).

Please let me know if you didn’t understand something. Hope this helps you!

2 Likes

Oh my gosh it worked! Thank you so much for your help! I would have never thought of this at all, lol. You’re a lifesaver!

1 Like

Thank you so much for your persistent help! I probably would have given up if it weren’t for your first message that encouraged me that this could be solved, lol :sob:

1 Like

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