Help to make a cycle for crediting money to the player who captured the point

I have a game where when a person touches a point, his nickname is displayed in a special value at the point.


Before touching, the value is equal to nothing.
image
For the points where the bloxy cola factories are located, I made a script with a loop that should check the value of the point and if there is someone’s nickname in the value, the factory should give me a bloxy cola.

while true do
	if script.Parent.Player.Value ~= nil then
		print("farm")
		local Players = game:GetService("Players")
		local ownername = script.Parent.Player.Value
		local owner = Players:FindFirstChild(ownername)
		owner.leaderstats.Bloxy = owner.leaderstats.Bloxy + 1
		wait(5)
	end
end

For some reason, nothing works, even the print does not give out anything. Thanks in advance.
image

P.S. I found an error in the output, but I can’t figure out how to solve it.

When using a “While loop”, you must use a wait(). Since you have a wait() within an If Statement, it will only fire when the values are met for the If Statement. Try this…

while true do
	if script.Parent.Player.Value ~= nil then
		print("farm")
		local Players = game:GetService("Players")
		local ownername = script.Parent.Player.Value
		local owner = Players:FindFirstChild(ownername)
		owner.leaderstats.Bloxy = owner.leaderstats.Bloxy + 1
		wait(5)
	end
    wait()
 end
1 Like

You could also just use a GetPropertyChangedSignal function. Simply just replace “while true do” with “script.Parent.Player:GetPropertyChangedSignal(“Value”):Connect(function()” and you don’t even need the wait().

1 Like

I tried your option and now another error has appeared.

it’s the same with the other way.


(But the past is gone)

hahahaha, I wanted to sleep when I was writing this script, and today, with a fresh head, I realized that I forgot to specify .Value in line 9 and also incorrectly specified the path to the currency, I also changed nil to " " and everything worked.


image
here’s the final script if anyone needs it.

while true do

	if script.Parent.Player.Value ~= "" then
		print("farm")
		local Players = game:GetService("Players")
		local ownername = script.Parent.Player.Value
		local owner = Players:FindFirstChild(ownername)
		owner.leaderstats.Bloxys.Value = owner.leaderstats.Bloxys.Value + 1
		wait(5)
	end
	wait()
end

I still recommend using GetPropertyChangedSignal, as using too many while true loops can start to lag your game. It should become a common practice to use alternatives whenever possible. Other than that, good job on finding the error! I can’t tell you how many times I’ve tried to change a Value without typing “.Value”…

1 Like

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