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 10 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:

01model = script.Parent.Factory
02backup = model:clone()
03 
04function Regen()
05    model:remove()
06    wait(1)
07    model = backup:clone()
08    model.Parent = script.Parent
09    model:makeJoints()
10end
11 
12while true do
13    wait(1.5)
14    if script.Parent:WaitForChild("Factory").OwnerName.Value ~= "" then
15        if not game.Players:FindFirstChild(script.Parent.Factory.OwnerName.Value) then
16        Regen()
17 
18        end
19    end
20end
0
Is there anything in the output? woodengop 1134 — 10y
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 — 10y
0
How did it bug? Bricks tweaked? woodengop 1134 — 10y
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 — 10y
View all comments (2 more)
0
I believe the bug came from the -loop- woodengop 1134 — 10y
0
Probably did, there is no other way of this looping, is there a way to fix this? excellentAnarchy 50 — 10y

1 answer

Log in to vote
5
Answered by 10 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.

01function FactoryFind(place, name)
02    for _,v in pairs(place:GetChildren()) do
03        if v.Name == "Factory" then -- Finding the factory
04            if v.Factory.OwnerName.Value == name then
05                v.Factory.OwnerName.Value = "" -- Setting name to nothing
06            end
07        else
08            FactoryFind(v) -- If we didn't find factory here, go deeper.
09        end
10    end
11end
12 
13game.Players.PlayerLeaving:connect(function (oldPlayer) -- Don't have access to wiki to make sure this is spelled/used right.
14    FactoryFind(Workspace, oldPlayer.Name)
15end)

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.

01model = script.Parent.Factory
02backup = model:clone()
03ownerName = script.Parent.Factory.OwnerName
04 
05function Regen()
06    model:remove()
07    wait(1)
08    model = backup:clone()
09    model.Parent = script.Parent
10    model:makeJoints()
11end
12 
13ownerName.Changed:connect(function (onChange)
14    if ownerName.Value == "" then
15        Regen
16    end
17end)

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 — 10y
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 — 10y
0
Right, let's add that to the game. excellentAnarchy 50 — 10y
0
That works perfectly :) excellentAnarchy 50 — 10y
0
Great! AmericanStripes 610 — 10y
Ad

Answer this question