This script is supposed to put into and weld an object called WhiteBox into player’s torso. Upon entering it works as it should, but when player dies this error appears (and WhiteBox doesn’t get putted into torso). halp
local whitebox = game.ReplicatedStorage.WhiteBox
Players = game:GetService("Players")
RunService =game:GetService("RunService")
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local weld = Instance.new("WeldConstraint")
weld.Parent = character.Torso
whitebox:Clone()
whitebox.Parent = character.Torso
local WB = character.Torso.WhiteBox
local Torso = character.Torso
weld.Part0 = Torso
weld.Part1 = WB
end)
end)
local repstorage = game:GetService("ReplicatedStorage")
local whitebox = repstorage:WaitForChild("WhiteBox")
local players = game:GetService("Players")
local runService =game:GetService("RunService")
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local weld = Instance.new("WeldConstraint")
local torso = character:WaitForChild("Torso")
weld.Parent = torso
local whiteboxClone = whitebox:Clone()
whiteboxClone.Parent = torso
weld.Part0 = torso
weld.Part1 = whiteboxClone
end)
end)
This is cleaner, I’ve removed unnecessary declarations, such as “WB” as it already exists as “whitebox” in your script. I’ve also added waits to prevent the script trying to execute before required instances have loaded. But yeah, your issue was that you were cloning the whitebox but not assigning it anywhere, so where you next parented the whitebox you were parenting the original whitebox and not the clone.
local WB = character.Torso.WhiteBox
local whiteboxClone = whitebox:Clone()
These are both referencing exactly the same instance, the clone of the whitebox, so no it isn’t necessary to declare a variable twice for the same instance. A clone is made for each player’s character every time they respawn. Try to go through the logic in my code & you’ll understand.
local whitebox = whitebox:Clone()
whitebox.Parent = character.Torso
local WB = character.Torso.WhiteBox
Notice how whitebox and WB are referencing the same instance? This is your script with the fix Zach added (assigning the clone to a variable).
Also it’s better to declare variables locally, in your original script you had declared “Players” as a global, declaring variables locally makes them part of the local environment and as such they can be accessed quicker.