Tweening Error; 'Can only tween objects in workspace'

I know this is not a very efficient way of making GUI’s but, when I try to tween, as you can see at the bottom, it says ‘Can only tween objects in workspace’. This error only happens at another place, not my original testing place. Any idea why this is happening? (I’ve removed the remote event firing/command that executes it and I can assure you at works)

-- Instances:
local raidDetectedGUI 		= Instance.new("ScreenGui", game.StarterGui)
local raidDetectedText 		= Instance.new("TextLabel", raidDetectedGUI)
local frameBackground 		= Instance.new("ImageLabel", raidDetectedGUI)

--Properties:
raidDetectedGUI.Archivable = false
raidDetectedGUI.Name = "raidDetectedGUI"
raidDetectedGUI.Parent = game.Players.LocalPlayer:WaitForChild("PlayerGui")

raidDetectedText.Name = "raidDetectedText"
raidDetectedText.Parent = raidDetectedGUI
raidDetectedText.BackgroundColor3 = Color3.new(1, 1, 1)
raidDetectedText.BackgroundTransparency = 1
raidDetectedText.Position = UDim2.new(1, 475, 0, 150)
raidDetectedText.Size = UDim2.new(0, 730, 0, 20)
raidDetectedText.ZIndex = 2
raidDetectedText.Font = Enum.Font.SciFi
raidDetectedText.Text = " dssdfsdfsdsdfsdfsdfsdsdf "
raidDetectedText.TextColor3 = Color3.new(1, 0, 0)
raidDetectedText.TextScaled = true
raidDetectedText.TextSize = 14
raidDetectedText.TextWrapped = true

frameBackground.Name = "frameBackground"
frameBackground.Parent = raidDetectedGUI
frameBackground.Active = true
frameBackground.BackgroundColor3 = Color3.new(1, 1, 1)
frameBackground.BackgroundTransparency = 1
frameBackground.Position = UDim2.new(1, 500, 0, 130)
frameBackground.Size = UDim2.new(0, 680, 0, 60)
frameBackground.Image = "rbxgameasset://Images/us"

raidDetectedGUI.Enabled = true

detectRaid.OnClientEvent:Connect(function(msg)
   raidDetectedText:TweenPosition(UDim2.new(0, 475, 0, 150), "In", "Linear", 1.1)
    frameBackground:TweenPosition(UDim2.new(0, 500, 0, 130), "In", "Linear", 1.1)
    raidDetectedText.Text 	= msg
    wait(6)
    raidDetectedText:TweenPosition(UDim2.new(1, 475, 0, 150), "In", "Linear", 1.1)
    frameBackground:TweenPosition(UDim2.new(1, 500, 0, 130), "In", "Linear", 1.1)
end)
4 Likes

That error happens when you try to tween objects that are parented to nil. Just make sure your GUI is in PlayerGui before you try to tween it.

20 Likes

Maybe waiting for the player to be in the game first will solve the issue?

5 Likes

Following up to what @Exodus_Reaper said just add this line at the top:

repeat wait() until game.Players.LocalPlayer.Character:FindFirstChild("Humanoid") ~= nil

Please guve Exodus the solution if what he said fixed your issue, not to me!!!

2 Likes

That would not work (you need to use FindFirstChild() to see if something is nil :wink: )

If you need to see if the player’s character exists something like

repeat wait() until game.Players.LocalPlayer.Character   

Would be enough.

Either way, I do agree that @Exodus_Reaper is probably right.

5 Likes

repeat wait() until game.Players.LocalPlayer.Character.Humanoid ~= nil

This will error if the player does not have a character, you could fix this by doing

repeat wait() until game.Players.LocalPlayer
local player = game.Players.LocalPlayer
repeat wait() until player and (player.Character or player.CharacterAdded:Wait()) and player.Character:FindFirstChild("Humanoid")
3 Likes

You don’t need to wait for LocalPlayer. It is guaranteed to exist for your local scripts you make as a developer. There are cases where it won’t exist yet, but those only apply to certain core scripts, meaning it has no impact on us whatsoever when writing code.

Also, you make your player variable, and then after that you start checking for it in the second loop, which is redundant. The player variable is going to be whatever you set it to on the second line; it won’t just change in the second repeat.

1 Like

repeat wait() until is an ugly, slow, and usually unneeded anti-pattern.

(player.Character or Player.CharacterAdded:wait()):WaitForChild("Humanoid")

…is all you need.

9 Likes

You don’t need to wait for LocalPlayer. It is guaranteed to exist for your local scripts you make as a developer.

Right. that is a recent change, I do it out of habit but not necessary.

It isn’t a recent change. The thread they posted was to announce the changed behavior for core scripts where it won’t exist yet. It was always guaranteed for as long as I know (my first experience being in 2013), since I have never had to wait for it.

2 Likes