Detecting number only woks when its one above

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Im trying to make a killstreak system that activates certain phases when you hit a number of kills

  2. What is the issue? Include screenshots / videos if possible!

this code below, only runs when the kills reach 6, but not 5 (5 being the intended trigger)

	elseif kills >= 5 and kills < 10 then
		print("5")
		EquipMod.Basic(phase1.Particles,char)
	else

as clearly shown, its supposed to run when its more then OR equal to 5 (shown by “>=”) and under 10

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    ive tried seperating it by making it an or statment, (for instance: if kills == 5 or kills > 5), still had same behavior

and yes, the kills value is correctly updating, i checked.

If you require any info, please ask!

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Have you another condition using kills = 5 or smth else with these numbers ?

Like if you have already a condition with 5 it’s may causes issue:

for exemple:

if kills >= 0 and kills < 5 then
		-- Script here
elseif kills >= 5 and kills < 10 then
		print("5")
		EquipMod.Basic(phase1.Particles,char)
else

Maybe it’s causes an error.
You may try:

if kills >= 0 and kills =< 4 then
		-- Script here
elseif kills >= 5 and kills < 10 then
		print("5")
		EquipMod.Basic(phase1.Particles,char)
else

To be sure it’s not that kind of problem.

2 Likes

Its turns out i did, the flag i had for detecting 1 through 5 had a <= 5 rather then just < 5 ;-;

sorry for the silly waste of time, but thank you!

2 Likes

Indexing a dictionary would be a better approach, in my opinion. You can change it the way you like.

local kills = 0

local arr = {
	[10] = function() print("10 kills") end,
	[30] = function() print("30 kills") end,
	[50] = function() print("50 kills") end,
}

while kills < 60 do
	local fn = arr[kills]
	if fn then
		fn();
	end
	kills += 1
end

thats a nice approach to it, i made my original system a while back and i was just improving the code, ill go ahead and try this method!

1 Like

and, by any chance would there be a way to fire the function if the number is skipped, sometimes if the suer gains kills to quickly, roblox dosent register it, so id like to make sure that the function is fired, even if its in-between

That depends on how you build your system.

One way to achieve that is by using signals.

local arr = {
	[10] = function() print("10 kills") end,
	[30] = function() print("30 kills") end,
	[50] = function() print("50 kills") end,
}

OnKillsChanged:Connect(function(value)
      local fn = arr[value]
      if fn then fn() end;
end)

the system is in a script in serverscriptservice, fired by a bindableevent

Yeah bindable events can achieve the same thing as the code above. However, custom signals are generally better because they use less threads and don’t clone the arguments passed to the Fire method.

You can also use a function.

-- sending a negative value would reduce kills
-- sending a positive value would increase kills
local function setKills(value: number)
     -- use assertions to keep the function robust
     kills += value
     local fn = arr[value]
     if fn then fn() end;
end

what do you mean by “Custom Signals”?

Custom signals is an implementation of the BindableEvent Instance but with code, which in return gives much better performance because it doesn’t have the nuances BindableEvents have in their implementation.

i looked a little bit into it, seems pretty interesting, though im not that advanced into scripting yet na dits a bit of a struggle for me to understand, but i have it saved, and ill look more into it when i can!

though it ahs given me the idea to use modules rather then bindable’s

1 Like

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