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

1 comment
It simply doesn't make sense making 2 else. Simply use elseif and then else, because computer would not understand if you put else and then else again.