Change a model's parent using a local script

I’m trying to change a model’s parent based on a leaderstat’s value but when i play the script doesn’t change it’s parent.

this is what i tried

if game.Players.LocalPlayer.Roomitems.Spot1.Value==2 then
		script.Parent.Parent=game.Workspace
3 Likes

It is because you don’t reference the leaderstats

if game.Players.LocalPlayer.leaderstats.Roomitems.Spot1.Value==2 then
script.Parent.Parent=game.Workspace
End

2 Likes

I’d recommend creating variables before you do actually start creating your if statement so that it’s easier to read & good practice:

local Player = game.Players.LocalPlayer
local leaderstats = Player:WaitForChild("leaderstats")

if leaderstats.Roomitems.Spot1.Value == 2 then
    script.Parent.Parent = workspace
end 

You have to fire it when it changed!!!
It only fires on enter, which won’t work when the value changes!
This is the fixed script:

local player = game:GetService(“Players”).LocalPlayer
local Value = player:WaitForChild(“Roomitems”).Spot1

local Model = script.Parent.Parent

Value.Changed:Connect(function()
if Value.Value == 2 then
Model.Parent = workspace
end
end)

Let me know if something doesn’t work!

1 Like