Need help with getting a value within a part that touched a part

Hey devs, I am wondering if anyone can help me sort this mess out. The game I am working on allows for mystery boxes around the map that when touched, they set their own “Active” value to false and go transparent. On the flip side, the vehicle the player drives has a mystery box item system that calls certain events on a random basis when the item box is touched.
My issue is, almost 2/3 of the item boxes I have on the map work. They are doing their job, but for some reason the touch event in the vehicle only detects the values of like 3 out of 10 boxes.

basepart.Touched:Connect(function(other)
	if other.Name == ("MysteryBox") and driverSeat.abilityget.Value == false then
		print("Passed Initial Check")
		local boxval = other:findFirstChild("Active")
		if boxval.Value == true then
			print("Boxval Passed")
			driverSeat.abilityget.Value = true
			sounds["Space Chimes"]:Play()
			fx.GeneralAbilities.Stars.Enabled = true
			task.wait(4)
			sounds["Enemy Gadget - Brawl Stars SFX"]:Play()
			driverSeat.abilityget.Value = true
			task.wait()
			fx.GeneralAbilities.Stars.Enabled = false
			local number = math.random(1,7)
			task.wait()
			abilnumber.Value = number
			if number == 1 then
				abilityPrompt.ObjectText = "You got: Overload Ability!"
			elseif number == 2 then
				abilityPrompt.ObjectText = "You got: Damage Ring Ability!"
			elseif number == 3 then
				abilityPrompt.ObjectText = "You got: Fire Ball Ability!"
			elseif number == 4 then
				abilityPrompt.ObjectText = "You got: Ice Cube Ability!"
			elseif number == 5 then
				abilityPrompt.ObjectText = "You got: SUPER Ability!"
			elseif number == 6 then
				abilityPrompt.ObjectText = "You got: Blizzard Ability!"
			elseif number == 7 then
				abilityPrompt.ObjectText = "You got: Bomb Ability!"
			end
			abilityPrompt.Enabled = true
		else
			print("Boxval Failed")
		end	
	
	end
end)

Am I missing something?

2 Likes

Touched is very reliant on server ping. Use GetPartsInPart instead (make another box which counts as the “BoundBox”) and use that to detect.

Oh and by the way, the idea seems somewhat interesting. Good luck developing!

2 Likes

thanks ill look into the getpartsinpart, and yeah I have a playable version, it’s just the updates im doing now require more script changes that I was not prepared for lol
You can find it here if you’re interested: Ball Wars - Roblox

1 Like

GetPartsBoundInBox also works if you don’t want to make a physical box. You do have to specify the size and position of this box, however.

(Small note: On the documentation, WorldRoot just means the game / workspace. As far as I know I think.)

1 Like

the item boxes are in based on their outter box so I have that going for me lol, I am just worried if the boxes are in a folder in the map, when the map changes there will be different boxes .

1 Like

What do you mean by “outer box”?

1 Like

the mystery boxes are comprised of 4 parts, an outer box, inner box, the ? mesh and an invisible .1x.1x.1 part that is hinged to the rest to make it spin. the outer box is what is being called for touch.
image

1 Like

Easy, then. Just call GetPartsInPart with the detection part in question being the Outer Box. (For best results, turn collision off on the Outer Box)

1 Like

And unrelated; but how did you make that box with only the outline and no fill? I’ve wondered on how to do that, but I have only managed to do it with multiple parts, which would be very unoptimized.

1 Like

It’s just a mesh I found in the free meshes called cube wire frame lol

1 Like

However, even still. How I have it now the script for the touch does return that it has touched the cubes. It just isn’t finding that boolean value within the cubes. Would GetPartsinParts really fix that?

1 Like

If anyone knows how I could apply get parts in parts in this script I am all ears. I tried looking it up and it seems simple but it doesn’t seem like it’ll do what I need it to do.

1 Like

Upon a lot of web searching and trial and error, I have found a solution to my issue! Hopefully to anyone who stumbles upon this will find it very helpful.
To find a boolValue from a part that is being Touched(), this is what I did:

basepart.Touched:Connect(function(other)

	local touchedBoolValue = other:FindFirstChild("Active")
	if touchedBoolValue then
			local boolValueState = touchedBoolValue.Value 
	if other.Name == ("MysteryBox") and boolValueState == true and driverSeat.abilityget.Value == false then
	
1 Like

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