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

[SOLVED] Randomized X position always ending up in one place after one randomize?

Asked by 4 years ago
Edited 4 years ago

In my previous question, I asked how I would make a randomized tree position every time a player chops it down. Fortunately, it was answered.

But the only thing is, that the after one randomized position, the tree always respawns in the same location. I have no idea why this happens, but it may be because of the code that was provided to me.

My current script:

local random = math.random(-100,100)
script.Parent.Changed:Connect(function()
    if script.Parent.Value <= 0 then
        script.Parent.Parent.CanCollide = false
        script.Parent.Parent.Transparency = 1
        script.Parent.Parent.Name = "cooling"
        wait(10)
        script.Parent.Parent.Name = "tree"
        script.Parent.Parent.Transparency = 0
        script.Parent.Value = 3
        script.Parent.Parent.CanCollide = true
        script.Parent.Parent.Position = Vector3.new(random,script.Parent.Parent.Position.Y,random);
    end
end)

The script randomizes the first position of the tree, then it always respawns at the first randomized position.

Any help please?

2 answers

Log in to vote
1
Answered by 4 years ago

Could this be because the random variable is defined at the beginning of the script so each time it runs it is the same. If you don’t want it to be the same move the location the variable is defined.

I apologize if I’m reading your script wrong but this might be the problem.

Happy Holidays! :)

0
You too! :D Sensei_Developer 298 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Setting the random as a variable will make it the same all the time. If you want to something like a random variable you need to make a function but it would just be easier to do Vector3.new(math.random,math.random,math.random)

0
Hmmmm... Seems reasonable. I'll try. Sensei_Developer 298 — 4y
0
Alright User_3805 6 — 4y
0
Join the discord User_3805 6 — 4y

Answer this question