Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

What might be wrong with this code? [ANSWERED]

Asked by 9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

What might be wrong with the codes below which make the tycoon bug, and reset over and over after a player leaves the game?

When game left:

model = script.Parent.Factory
backup = model:clone() 

function Regen() 
    model:remove() 
    wait(1) 
    model = backup:clone() 
    model.Parent = script.Parent 
    model:makeJoints() 
end 

while true do 
    wait(1.5) 
    if script.Parent:WaitForChild("Factory").OwnerName.Value ~= "" then 
        if not game.Players:FindFirstChild(script.Parent.Factory.OwnerName.Value) then
        Regen() 

        end 
    end 
end 
0
Is there anything in the output? woodengop 1134 — 9y
0
The code works, I've tested it in a server via studio, when I left, everything regened nicely, but while testing with more players, it bugged once. excellentAnarchy 50 — 9y
0
How did it bug? Bricks tweaked? woodengop 1134 — 9y
0
It regenerated in loops, even when I became an owner of that House, it said previous owners name + mine at the same time, the whole "tycoon" blinked with bought, and not yet bought items. excellentAnarchy 50 — 9y
View all comments (2 more)
0
I believe the bug came from the -loop- woodengop 1134 — 9y
0
Probably did, there is no other way of this looping, is there a way to fix this? excellentAnarchy 50 — 9y

1 answer

Log in to vote
5
Answered by 9 years ago

Let's take a step back and think here.

What could we do differently besides a loop, to make it more effective?


First

We need to create a new script in Workspace that will set the owner's name to nothing in the event he leaves. This is simple, so I won't go into to much detail other than the comments I leave.

function FactoryFind(place, name)
    for _,v in pairs(place:GetChildren()) do
        if v.Name == "Factory" then -- Finding the factory
            if v.Factory.OwnerName.Value == name then
                v.Factory.OwnerName.Value = "" -- Setting name to nothing
            end
        else
            FactoryFind(v) -- If we didn't find factory here, go deeper.
        end
    end
end

game.Players.PlayerLeaving:connect(function (oldPlayer) -- Don't have access to wiki to make sure this is spelled/used right.
    FactoryFind(Workspace, oldPlayer.Name)
end) 

Now,

All we need to do is edit your current script to used the changed event. Let's first create the event:

Note: We are going to keep your regen and variables for consistency.

model = script.Parent.Factory
backup = model:clone() 
ownerName = script.Parent.Factory.OwnerName

function Regen() 
    model:remove() 
    wait(1) 
    model = backup:clone() 
    model.Parent = script.Parent 
    model:makeJoints() 
end 

ownerName.Changed:connect(function (onChange)
    if ownerName.Value == "" then
        Regen
    end
end)

Finally..

In theory, the code I have supplied you, and with the ways mentioned, you can figure out how to consider other possible means yourself in the future. Just need to always keep thinking, and remember, try to solve the problem, not just cover it.

Note: I have done absolutely no testing with this. I am simply assuming the code given will work, as I do not have studio right now to run any tests on.

0
Yes, but there isn't just one "Factory" and the first script would change the Owner Value of a different "Factory" wouldn't it? excellentAnarchy 50 — 9y
0
No, because our If statement catches that. The if statement would make sure the Value of the door is in fact the name of the oldPlayers, and then reset it. AmericanStripes 610 — 9y
0
Right, let's add that to the game. excellentAnarchy 50 — 9y
0
That works perfectly :) excellentAnarchy 50 — 9y
0
Great! AmericanStripes 610 — 9y
Ad

Answer this question