Help with a script

What i want to make a block, where if pressed you get +1 value/balance and it moves its position afterward.

The issue is that my code only moves the part when clicked once. if i try and click again, it does not change its position
i dont get why it only changes its position once and wont do it again?

i tried making a loop, couldnt figure out how to really make it a loop. ive tried looking for solutions on here but couldnt find anything really
im relatively new to scripting, i would love any feedback or tips.

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
 leaderstats.Name = "leaderstats"

local balance = Instance.new("IntValue")
	balance.Name = "GrassPoints™"
	balance.Parent = leaderstats
	
	leaderstats.Parent = plr
end)



	local clickpart = game.Workspace:WaitForChild("grass")

local grassmaxxpos = 46
local grassmaxypos = 0
local grassmaxzpos = 0

local grassxpos = math.random(1, grassmaxxpos)
local grassypos = 0
local grasszpos = 0

workspace.grass.ClickDetector.MouseClick:Connect(function(plr)

	local point = plr:WaitForChild("leaderstats"):WaitForChild("GrassPoints™")
point.Value += 1

workspace.grass.Position = Vector3.new(grassxpos, grassypos, grasszpos)

end)

You are creating a random number and then using that same number every time, instead of generating a new one, your code should look like this:

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
 leaderstats.Name = "leaderstats"

local balance = Instance.new("IntValue")
	balance.Name = "GrassPoints™"
	balance.Parent = leaderstats
	
	leaderstats.Parent = plr
end)



	local clickpart = game.Workspace:WaitForChild("grass")

local grassmaxxpos = 46
local grassmaxypos = 0
local grassmaxzpos = 0



workspace.grass.ClickDetector.MouseClick:Connect(function(plr)

	local grassxpos = math.random(1, grassmaxxpos)
	local grassypos = 0
	local grasszpos = 0

	local point = plr:WaitForChild("leaderstats"):WaitForChild("GrassPoints™")
	point.Value += 1

	workspace.grass.Position = Vector3.new(grassxpos, grassypos, grasszpos)

end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.