Greetings,
I've made a script to generate a random wheat block from a list, but when you click multiple wheat blocks, it seems to choose always the same block. How can I fix that?
Main Script
local wheatGroup = workspace:WaitForChild("WheatFolder") local genRandomWheat = game:GetService("ReplicatedStorage"):WaitForChild("GenRandomWheat") local function generateRandom() local child = workspace.WheatFolder:GetChildren() print(#child) local x = 70/100*#child local y = math.floor(x) print(y) for i = 1,y do child[i].Transparency = 1 child[i].CanCollide = false child[i]:WaitForChild("IsHarvested").Value = true child[i]:WaitForChild("ClickDetector").MaxActivationDistance = 0 end end local function generateOneRandom() local child = workspace.WheatFolder:GetChildren() local x = #child/#child for i = 1,x do child[i].Transparency = 0 child[i].CanCollide = true child[i]:WaitForChild("IsHarvested").Value = false child[i]:WaitForChild("ClickDetector").MaxActivationDistance = 10 end end game.Players.PlayerAdded:Connect(generateRandom) genRandomWheat.Event:Connect(generateOneRandom)
Every wheat script
local wheat = script.Parent.Parent local clickDetector = wheat:WaitForChild("ClickDetector") local isHarvested = wheat:WaitForChild("IsHarvested") local genRandomWheat = game:GetService("ReplicatedStorage"):WaitForChild("GenRandomWheat") local function harvest(player) if player then local playerRubbles = player.leaderstats:WaitForChild("Rubbles") if isHarvested.Value == false then playerRubbles.Value = playerRubbles.Value + 10 isHarvested.Value = true wheat.Transparency = 1 wheat.CanCollide = false clickDetector.MaxActivationDistance = 0 genRandomWheat:Fire() else warn("Block is already harvested. Please harvest another time.") end else error("Player not detected. Please contact Time_URSS for this error. ERROR#051") end end clickDetector.MouseClick:Connect(harvest)
I can track the error over the line 20
You got to spawn in a block with a script when it grows back and then scale it and Material it to make it look like wheat or have it already made in Storage and clone it into workspace after grow.
at line 6 u need to remove a local
Nvm, problem fixed. I had to change some things at lines 7 and 20, so now it's working perfectly. Thanks for your help!