Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to randomize the X position of a part?

I'm making a tree-chop script, and when the player successfully chops down a tree, the tree will take 10 seconds to respawn in a different location.

Here is my 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.Position.X = random
	end
end)

Output is: X cannot be assigned to

Any help please?

1 answer

✓ Accepted answer

Position is a Vector3 value, you can't overwrite individual parts of it (e.g. the X). You can read them, but you can't write to them directly.

What you can do is set the Position to a new Vector3 where the Y and the Z are the same as before.

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.Position = Vector3.new(random, script.Parent.Parent.Position.Y, script.Parent.Parent.Position.Z);
    end
end)

1 comment

0

Thanks, but the thing is, it keeps spawning at the area it randomized first, and I have no idea why that happens. Perhaps you can edit your post so it'll fix the issue? Thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.