Return not working?

Hi. I have no idea why this script keeps on going even when I put return. After the time reaches 100 I don’t want it to check for that anymore. Any help is appreicated.

LocalPlayer.leaderstats.Time.Changed:Connect(function()
	repeat wait(1) until LocalPlayer.leaderstats
	if LocalPlayer.leaderstats.Time.Value >= 100 then
		print("player has 10 or more time")
		remote:FireServer("Gold")
		return
	end
	if LocalPlayer.leaderstats.Time.Value >= 500 then
		remote:FireServer("Anger")
		return
	end
	if LocalPlayer.leaderstats.Time.Value >= 750 then
		remote:FireServer("Birthday")
	end
	if LocalPlayer.leaderstats.Time.Value >= 1000 then
		remote:FireServer("Love Struck")
	end
	if LocalPlayer.leaderstats.Time.Value >= 1800 then
		remote:FireServer("Plasma")
	end
	if LocalPlayer.leaderstats.Time.Value >= 5400 then
		remote:FireServer("Current")
	end
	if LocalPlayer.leaderstats.Time.Value >= 8000 then
		remote:FireServer("Sandstorm")
	end
	if LocalPlayer.leaderstats.Time.Value >= 9500 then
		remote:FireServer("Breeze")
	end
	if LocalPlayer.leaderstats.Time.Value >= 15000  then
		remote:FireServer("Stormbringer")
	end
end)

HI Mate.

Looking at the script once it reaches the 100 it runs the remote for “gold” then will not run again as the player does not meet the next amount of 500. have you consdered merging together so if you meet the amount the script will run to the next wait in which when the player meets this amount it will then run and so on.

Or add the repeat wait to the start of each time time value as you only have the script waiting for the for value of 100.

repeat wait(1) until LocalPlayer.leaderstats
if LocalPlayer.leaderstats.Time.Value >= 100 then
remote:FireServer(“Gold”)
end

repeat wait(1) until LocalPlayer.leaderstats
if LocalPlayer.leaderstats.Time.Value >= 500 then
remote:FireServer(“Anger”)
end

etc…etc…

1 Like

This is because if you have (for example) 500 time, then the script will only run the 100 time statement and will end because of the “return” keyword you put.

The solution is to either remove the “return” keyword or to use math.clamp.

math.clamp(number,min,max) is a function that returns the number specified if the number is between the range specified. With this function we can make an easier alternative to if x > min and x <= max then by using if math.clamp(x,min,max) == x then.

So now the script should look like this:

LocalPlayer.leaderstats.Time.Changed:Connect(function()
	repeat wait(1) until LocalPlayer.leaderstats
	local x = LocalPlayer.leaderstats.Time.Value
	if math.clamp(x,-math.huge,100) == x then --I set the minimum as negative infinity since there's no limit
		print("player has 10 or more time")
		remote:FireServer("Gold")
	end
	if math.clamp(x,-math.huge,500) == x then
		remote:FireServer("Anger")
		return
	end
	if math.clamp(x,-math.huge,750) == x then
		remote:FireServer("Birthday")
	end
	if math.clamp(x,-math.huge,1000) == x then
		remote:FireServer("Love Struck")
	end
	if math.clamp(x,-math.huge,1800) == x then
		remote:FireServer("Plasma")
	end
	if math.clamp(x,-math.huge,5400) == x then
		remote:FireServer("Current")
	end
	if math.clamp(x,-math.huge,8000) == x then
		remote:FireServer("Sandstorm")
	end
	if math.clamp(x,-math.huge,9500) == x then
		remote:FireServer("Breeze")
	end
	if math.clamp(x,-math.huge,15000) == x then
		remote:FireServer("Stormbringer")
	end
end)

Thanks this works for what I posted here but then there is another issue. If I join the game and give myself 1000 time I will not get all the swords that are available up to that time. I don’t really get math.clamp so I’m thinking its with that?

You will get all the swords. I thought the remote event was used to equip a sword

It does

remote.OnServerEvent:Connect(function(client, request)
	--print("event was fired")
	local inventory_folder = inventories[client.Name]
	local tool = tools[request]
	if inventory_folder:FindFirstChild(tool.Name) then
     --print("player already has sword")
	else
		local profile = ProfileCache[client]
		if profile ~= nil then 
			if tool:GetAttribute("TypeOfRequirement") == "Time" then
				--print('is time')
				if profile.Data.Time > tool:GetAttribute("Requirement") then
					--print("giving sword")
					tool:Clone().Parent = client.Backpack
					tool:Clone().Parent = inventory_folder	
				end
			else
				if tool:GetAttribute("TypeOfRequirement") == "Kills" then
					--print('is kills')
					if profile.Data.Time > tool:GetAttribute("Requirement") then
						--print("giving sword")
						tool:Clone().Parent = client.Backpack
						tool:Clone().Parent = inventory_folder	
					end
				end
			end
		end
	end
end)

https://gyazo.com/685832c7a17530d1c54cf3e40b8e156c

Very subtle difference in code but you could be using elseif, and return does return what is passed and is only used for functions that are intended to return a value (such as CFrame.new).

local players = game:GetService("Players")
local localplayer = players.LocalPlayer

local leaderstats = localplayer:WaitForChild("leaderstats")
local time = leaderstats:WaitForChild("Time")

time:GetPropertyChangedSignal("Value"):Connect(function()
	local timeRemaining = time.Value
	if timeRemaining >= 100 then
		print'player has 100 or more time'
		return remote:FireServer("Gold")
	elseif timeRemaining >= 500 then
		return remote:FireServer("Anger")
	elseif timeRemaining >= 750 then
		return remote:FireServer("Birthday")
	elseif timeRemaining >= 1000 then
		return remote:FireServer("Love Struck")
	elseif timeRemaining >= 1800 then
		return remote:FireServer("Current")
	elseif timeRemaining >= 8000 then
		return remote:FIreServer("Sandstorm")
	elseif timeRemaining >= 9500 then
		return remote:FireServer("Breeze")
	elseif timeRemaining >= 15000 then
		return remote:FireServer("Stormbringer")
	end
end)
1 Like

Oh and also use elseif like the reply stated above

That isn’t true, return can also be used to terminate a function’s execution process (which is what he’s trying to do here), if there’s no parameter for return then it would just be return nil.

It’s like using assert() but simpler and more efficient.

return is used for that scenario, yes. But pausing executing threads is not what return is for. In the naming convention, return, you are to use it to return a value. If you want to break a loop, you’d use the named declaration (not variable) break. Checking boolean states and using return to cancel out the function if those boolean states are false can easily be replaced by assertion.

The thing is that assertion does make a log in the output, which can be annoying if you’re trying to make a clean console log. While return doesn’t do that.

And so you’d use pcall, or you continue your thread with a positive check rather than a return, it saves you time in the writing process.

There are lots of different ways to achieve the same outcome. I have a similar system like this in my own game in which i have not used anything like in this thread but it still work which is the main thing :+1:

LocalPlayer.leaderstats.Time.Changed:Connect(function()
	repeat wait(1) until LocalPlayer.leaderstats
	local x = LocalPlayer.leaderstats.Time.Value
	if math.clamp(x,-math.huge,100) == x then
		print("player has 10 or more time")
		return remote:FireServer("Gold")
	elseif math.clamp(x,-math.huge,500) == x then
	return remote:FireServer("Anger")
	end
end)

This still doesn’t give both if they get 1000 time.

That is more complicated than using return though.

Oh it was supposed to give both? If so then keep using if instead

The good of not using return in such scenarios is fine to use in a code block.

if foo then --> false? -> skip chunk
    return "bar"
end
assert(foo) --> false? -> error
return "bar"
if not foo then --> false? -> stop thread
    return
end
return "bar"

Now you may see the problem with returning nothing if something isn’t true.

1 Like

So this?

time:GetPropertyChangedSignal("Value"):Connect(function()
	local timeRemaining = time.Value
	if timeRemaining >= 100 then
		print'player has 100 or more time'
		return remote:FireServer("Gold")
	end
		if timeRemaining >= 500 then
			return remote:FireServer("Anger")
		end

	end)

I’m trying to make it so if a player has enough for one sword and over give them that sword. But if the player has enough for all the swords then give them all the swords.

Then don’t use return, simple.