Empty string ("") is equal to a filled in one ("1Checkpoint") but only in one place?

Hi there. :grinning: (just got new member so excuse me if i’m dumb)

So i was thinking abaut a game, where you race on foot, (minigame but that’s not the point) and got stuck on something rather odd…

script.Parent.HumanoidRootPart.Touched:connect(function(hit)
	if hit.Name == workspace.MainCP.name then
		local Cycle = 0
		local AOK = false
		local FalseCount = 0
		repeat
			Cycle = Cycle + 1
			if _G.RacingPeeps[_G[script.Parent.Name.."Table"]][Cycle + 2] == "" then --this is the part
				FalseCount = FalseCount + 1
			end
		until _G.RacingPeeps[_G[script.Parent.Name.."Table"]][Cycle + 2] == nil
		Cycle = 0
		if FalseCount == 0 then
			repeat
				Cycle = Cycle + 1
			_G.RacingPeeps[_G[script.Parent.Name.."Table"]][Cycle + 2] = ""
			until Cycle == _G.ACP
			_G.RacingPeeps[_G[script.Parent.Name.."Table"]][2] = _G.RacingPeeps[_G[script.Parent.Name.."Table"]][2] + 1
		end
		print(_G.RacingPeeps[_G[script.Parent.Name.."Table"]][2])
	end
end)

So yeah

if _G.RacingPeeps[_G[script.Parent.Name.."Table"]][Cycle + 2] == "" then

returns true no matter what. :face_with_raised_eyebrow:
I even tried to have it fire only when it was false. (Basically count the checkpoints and if you have all it will give you a lap.) Unlike now where i check if it has some checkpoints missing.
Any chance you guys could help me? Or is this a lost cause?

If you need further info… ask.

1 Like

So what are you even assigning to that element in the table?
It seems you have both nil, strings and numbers in there.

I had to reduce the code a bit, to make it readable - instead of constantly seeing those long table-addressing statements:

	local Cycle = 0
	local AOK = false
	local FalseCount = 0
	
	local searchTable = _G.RacingPeeps[_G[script.Parent.Name.."Table"]]

	repeat
		Cycle = Cycle + 1
		if searchTable[Cycle + 2] == "" then
			FalseCount = FalseCount + 1
		end
	until searchTable[Cycle + 2] == nil

	if FalseCount == 0 then
		Cycle = 0
		repeat
			Cycle = Cycle + 1
			searchTable[Cycle + 2] = ""
		until Cycle == _G.ACP  -- What is 'ACP'? I suppose it is a number.
		searchTable[2] = searchTable[2] + 1  -- This is not a string, so what is it used for?
	end
	
	print(searchTable[2]) -- What information does this give you?

In your code you do two assignments.

First -

_G.RacingPeeps[_G[script.Parent.Name.."Table"]][Cycle + 2] = ""

(Which is where you set the value to “”)
and second

_G.RacingPeeps[_G[script.Parent.Name.."Table"]][2] = _G.RacingPeeps[_G[script.Parent.Name.."Table"]][2] + 1

I won’t lie. After taking a while to think about this, I have no idea what you tried to achieve.

Basically - On first hit of MainCP part (from Workspace) there seems to be no checkpoints applied so FalseCount = 0. That’s where the second repeat loop runs and assigns all checkpoints from 3 to _G.ATP to “”. That’s why it always does contain “”.

PS: Is this a script or localscript?

so… some explaining

no they are suposed to be (and are) checkpoint names set by another script… should i include the entire project so far? cuz i can if that’s easier

same answear but it’s a normal script

ok i am going to include it

something.rbxl (25.8 KB)

ps: still if the value was nill and i tried checking if it was “” then it would not do anything

I’ll supply you a fixed place then. Hold on few minutes and description of what I changed.
(I’ll only change the LapCount and CPCount script)

thx but i really only needed an explanation not a fixed place

1 Like

Okay. So about CPCount

script.Parent.HumanoidRootPart.Touched:connect(function(hit)
	if hit.Parent == workspace.Checkpoints then
		local Cycle = 0
		local Found = false
		local CCP = 0
		repeat
			Cycle = Cycle + 1
			if _G.RacingPeeps[_G[script.Parent.Name.."Table"]][Cycle + 2] == "" then
				CCP = CCP + 1
			elseif _G.RacingPeeps[_G[script.Parent.Name.."Table"]][Cycle + 2] == hit.Name then
				Found = true
			end
		until Found or CCP == _G.ACP
		if CCP == _G.ACP then
			local Cycle = 0
			repeat
				Cycle = Cycle + 1
			until _G.RacingPeeps[_G[script.Parent.Name.."Table"]][Cycle + 2] == nil
			_G.RacingPeeps[_G[script.Parent.Name.."Table"]][Cycle + 2] = hit.Name
		end
	end
end)

What you do is completely not needed.

script.Parent.HumanoidRootPart.Touched:connect(function(hit)
	if not (hit.Parent == game.Workspace:WaitForChild("Checkpoints")) then return end -- Do not accept any other instances than those parented to Checkpoints in Workspace

	local Number = hit.Name:match("(%d+)Checkpoint") -- Returns you the checkpoint number (%d stands for digit and it tries to find it)
	if not Number then return end -- Does not match the pattern. Is not a CP.

	_G.RacingPeeps[_G[script.Parent.Name.."Table"]][Number] = true -- Save that person reached the CP
end)

In previous script. That’s hard to think what you tried to make, but tried to simplify the process a lot. Now taking care of LapCount.

script.Parent.HumanoidRootPart.Touched:connect(function(hit)
	if hit.Name == workspace.MainCP.name then
		local Cycle = 0
		local AOK = false
		local FalseCount = 0
		repeat
			Cycle = Cycle + 1
			if _G.RacingPeeps[_G[script.Parent.Name.."Table"]][Cycle + 2] == "" then
				FalseCount = FalseCount + 1
			end
		until _G.RacingPeeps[_G[script.Parent.Name.."Table"]][Cycle + 2] == nil
		Cycle = 0
		if FalseCount == 0 then
			repeat
				Cycle = Cycle + 1
			_G.RacingPeeps[_G[script.Parent.Name.."Table"]][Cycle + 2] = ""
			until Cycle == _G.ACP
			_G.RacingPeeps[_G[script.Parent.Name.."Table"]][2] = _G.RacingPeeps[_G[script.Parent.Name.."Table"]][2] + 1
		end
		print(_G.RacingPeeps[_G[script.Parent.Name.."Table"]][2])
	end
end)

Changed to

script.Parent.HumanoidRootPart.Touched:connect(function(hit)
	if hit == game.Workspace:WaitForChild("MainCP") then -- Why to compare name when you can just compare the instance?
		for i = 1, _G.ACP do -- Check from 1 to total checkpoint count
			if not _G.RacingPeeps[_G[script.Parent.Name.."Table"]][i] then
				return -- Checkpoint not reached
			end
		end
		-- Clear all the checkpoint data now
		for i = 1, _G.ACP do -- Check from 1 to total checkpoint count
			_G.RacingPeeps[_G[script.Parent.Name.."Table"]][i] = nil
		end
		-- If one of checkpoints wasn't reached the function would exit already (on the return command). If it didn't, user passed all checkpoints.
		-- Do some lap assigning here. Make sure it does not conflict with CPs (_G.RacingPeeps[_G[script.Parent.Name.."Table"]][Number])
	end
end)
1 Like

ok thx but again i am new to this forum thing

No problem!

Few other notes:

  • Seems that _G.RacingPeeps[_G[script.Parent.Name.."Table"]] is sometimes nil. Don’t get surprised if an error pops up when you hit CP / MainCP. But that’s not big deal! just add _G.RacingPeeps[_G[script.Parent.Name.."Table"]] = {} somewhere! Same with _G[script.Parent.Name.."Table"]
  • You shouldn’t rely on global (_G) table.

If I helped with the issue, I would appreciate marking my response as the answer.
Also I fixed two typos.

Edit: Just realized I forgot to add checkpoint data clearing up. Updated the previous post. Should be perfect now. As said I did touch only CPCount and LapCount, so you might want to adjust the Workspace.Start and MyTable script to work with those changes.

1 Like

should i give credit (cuz you fixed it)
and the thing is… where is the lap number stored again? :grinning:
and the thing i tried was that i would
create a table of racing players from racing team
create a table for each (1.st value is the name and the 2.nd is the lap number and from 3 it’s the checkpoints)
write down the checkpoints touched (whitout duplication)
check if the checkpoints are there
if yes: empty the table from value 3 and up untill it hits nil and lap count goes up by one
if no: sit there existing

No need to give the credit. If you wish I can get the rest fixed for you (laps and things). I only touched the CP problem now. I left you a comment where to assign a lap.

i will return with results (good ones hopefully)
is _G.RacingPeeps[_G[script.Parent.Name…“Table”]][Number] the already touched cp number?

Yes. If you really wish to store the laps in the same table use
_G.RacingPeeps[_G[script.Parent.Name…“Table”]][_G.ACP + 1]
This uses value 1 greater than all checkpoints.

1 Like

WOW thx man… starting to like the forums already :grinning:
it works
I have a game where it’s a cluster of minigames you can choose from, and earn credit (G), and spend it, and that’s where this will end up in.

I have an already working model, with power-ups and all that, but it lacks the whole “position” part and only tracks wins (hence the rework).

Marked as solution… thx.

1 Like