Part's Touch Not Doing Anything

Hello forummers, I’ve been making a obby system in my game which players can teleport to obbies using portals, in which they get rewards for completing the obbies. Basically everything works, but for some reasons, when it comes to the touch of the endpart (which rewards the player), it’s not printing anything or rewarding me. This is mainly where the issue is:

This is my module script:

local en = require(game.ReplicatedStorage.Modules.EternityNum)

local PortalModule = {}

PortalModule.Portals = {
	{
		PortalName = "OverworldPortal",
		ObbyName = "OverworldObby",
		ValueName = "Multiplier",
		RequiredValue = en.convert(2),
		Color = Color3.fromRGB(0, 174, 255),
		Rewards = {
			{Amount = 100, Chance = 50, Currency = "Points"},
			{Amount = 200, Chance = 30, Currency = "Points"},
			{Amount = 500, Chance = 20, Currency = "Points"}
		}
	},
	{
		PortalName = "Portal2",
		ObbyName = "UnderworldObby",
		ValueName = "RequiredValue",
		RequiredValue = 20,
		Color = Color3.fromRGB(0, 255, 0),
		Rewards = {
			{Amount = 150, Chance = 40, Currency = "Coins"},
			{Amount = 300, Chance = 40, Currency = "Coins"},
			{Amount = 600, Chance = 20, Currency = "Coins"}
		}
	},
	-- Add more obby configurations as needed
}

return PortalModule

and this is my whole server script:

local en = require(game.ReplicatedStorage.Modules.EternityNum)
local ObbyModule = require(game.ReplicatedStorage.Modules.ObbyModule)
local TweenService = game:GetService("TweenService")

-- Function to choose a reward based on percentage chance
local function chooseReward(rewards)
	local totalChance = 0
	for _, reward in pairs(rewards) do
		totalChance = totalChance + reward.Chance
	end

	local randomChance = math.random() * totalChance
	local cumulativeChance = 0

	for _, reward in pairs(rewards) do
		cumulativeChance = cumulativeChance + reward.Chance
		if randomChance <= cumulativeChance then
			return reward
		end
	end

	return nil
end

-- Function to reward player
local function rewardPlayer(player, reward)
	if reward then
		local currencyValue = player.leaderstats:FindFirstChild(reward.Currency)
		if currencyValue then
			currencyValue.Value = currencyValue.Value + reward.Amount
		end
		print(player.Name .. " has been rewarded with " .. tostring(reward.Amount) .. " " .. reward.Currency)
	end
end

-- Function to handle teleportation logic
local function teleportPlayer(plr, portalConfig)
	print("Teleporting player:", plr.Name, "through portal:", portalConfig.PortalName)
	local human = plr.Character:FindFirstChild("Humanoid")
	if human then
		print("Humanoid found for player:", plr.Name)
		local pass = plr:WaitForChild("Data").Stats[portalConfig.ValueName]
		print("Player pass value:", pass.Value, "Required value:", en.toString(portalConfig.RequiredValue))
		if pass.Value >= en.toString(portalConfig.RequiredValue) then
			print("Player meets the requirement, starting teleportation")
			game.ReplicatedStorage.Remotes.ShowTeleportScreen:FireClient(plr, portalConfig.Color or Color3.new(1, 1, 1))

			wait(1.5)

			local teleportPart = workspace.Map.Obbies.ActualObbies:WaitForChild(portalConfig.ObbyName).StartPart
			plr.Character:SetPrimaryPartCFrame(CFrame.new(teleportPart.Position))
			print("Teleported player:", plr.Name, "to:", teleportPart.Position)


			game.ReplicatedStorage.Remotes.HideTeleportScreen:FireClient(plr)
		else
			print("Player does not meet the requirement")
		end
	else
		print("Humanoid not found for player:", plr.Name)
	end
end

-- Function to set up portal touch event
local function setupPortal(portal, portalConfig)
	portal.Touch.Touched:Connect(function(hit)
		print("Portal touched:", portal.Name)
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr then
			print("Player detected:", plr.Name)
			teleportPlayer(plr, portalConfig)
		else
			print("Player not detected from touch")
		end
	end)
end

-- Initialize all portals
local function initializePortals()
	for _, portalConfig in ipairs(ObbyModule.Portals) do
		print("Setting up portal:", portalConfig.PortalName)
		local portal = workspace.Map.Obbies.Portals:WaitForChild(portalConfig.PortalName)
		setupPortal(portal, portalConfig)
	end
	print("All portals have been set up.")
end

-- Call initialization function
initializePortals()

-- Handle teleport requests from UI buttons
game.ReplicatedStorage.Remotes.TeleportPlayerToLocation.OnServerEvent:Connect(function(plr, portalName)
	for _, portalConfig in ipairs(ObbyModule.Portals) do
		if portalConfig.PortalName == portalName then
			teleportPlayer(plr, portalConfig)
			break
		end
	end
end)

-- Handle obby completion
game.Workspace.Map.Obbies.DescendantAdded:Connect(function(descendant)
	if descendant:IsA("Part") and descendant.Name == "EndPart" then
		print("EndPart added:", descendant:GetFullName())
		descendant.Touched:Connect(function(hit)
			print("EndPart touched:", descendant.Name)
			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
			if plr then
				local obbyName = descendant.Parent.Name
				print("Player detected:", plr.Name, "at obby:", obbyName)
				for _, portalConfig in ipairs(ObbyModule.Portals) do
					if portalConfig.ObbyName == obbyName then
						print("Matching obby config found for:", obbyName)
						local reward = chooseReward(portalConfig.Rewards)
						if reward then
							rewardPlayer(plr, reward)
							print(plr.Name .. " has finished " .. obbyName .. " and received " .. reward.Amount .. " " .. reward.Currency)
						else
							print(plr.Name .. " has finished " .. obbyName .. " but received no reward")
						end
					end
				end
			else
				print("Player not detected from touch")
			end
		end)
	end
end)

3 Likes
local part = script.Parent

part.Touched:Connect(function(hit)
    -- This function will do nothing when the part is touched
end)

3 Likes

I would need to make it a function instead?

2 Likes

There is no prints whatsoever from the screenshot you posted? Not even the “EndPart added”?

If you’re testing it on studio, without StreamingEnabled, test it in a live game.

Chances are, you are attempting to wait for a descendant called EndPart to be added that’s already there by the time your code reaches that point.

Unless it’s added in by a script, it’s unreliable to rely on just an ObjectAdded type event.

This is why we don’t just use PlayerAdded when making Data systems, we also iterate the players because that catches any early Player creation as well.

unfortunally no, there isnt anything

StreamingEnabled is enabled in my game, yet i still am not receiving any prints or anything. I changed the script a bit, removing the DescendantAdded, but its still not working. this is my new script (feel free to let me know about changes i should make):

local function initializePortalsAndEndParts()
	for _, portalConfig in ipairs(ObbyModule.Portals) do
		print("Setting up portal:", portalConfig.PortalName)
		local portal = workspace.Map.Obbies.Portals:WaitForChild(portalConfig.PortalName)
		setupPortal(portal, portalConfig)
	end

	print("All portals have been set up.")

	for _, portalConfig in ipairs(ObbyModule.Portals) do
		local obby = workspace.Map.Obbies.ActualObbies:FindFirstChild(portalConfig.ObbyName)
		if obby then
			local endPart = obby:FindFirstChild("EndPart")
			if endPart then
				print("Setting up EndPart touch event for:", obby.Name)
				endPart.Touched:Connect(function(hit)
					print("EndPart touched:", endPart.Name)
					local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
					if plr then
						local obbyName = endPart.Parent.Name
						print("Player detected:", plr.Name, "at obby:", obbyName)
						for _, portalConfig in ipairs(ObbyModule.Portals) do
							if portalConfig.ObbyName == obbyName then
								print("Matching obby config found for:", obbyName)
								local reward = chooseReward(portalConfig.Rewards)
								if reward then
									rewardPlayer(plr, reward)
									print(plr.Name .. " has finished " .. obbyName .. " and received " .. reward.Amount .. " " .. reward.Currency)
								else
									print(plr.Name .. " has finished " .. obbyName .. " but received no reward")
								end
							end
						end
					else
						print("Player not detected from touch")
					end
				end)
			else
				print("EndPart not found for obby:", obby.Name)
			end
		else
			print("Obby not found:", portalConfig.ObbyName)
		end
	end
end

initializePortalsAndEndParts()

Can you send me a file containing the output from your console Output?
It should allow me to debug a little more effectively.

I actually figured out the issue, but now I’m coming across an error when i touch the part.

this is where im getting the error:

-- Function to reward player
local function rewardPlayer(player, reward)
	if reward then
		local currencyValue = player.Data.Stats:FindFirstChild(reward.Currency)
		if currencyValue then
			currencyValue.Value = currencyValue.Value + reward.Amount
		end
		print(player.Name .. " has been rewarded with " .. tostring(reward.Amount) .. " " .. reward.Currency)
	end
end

this is my module script:

local en = require(game.ReplicatedStorage.Modules.EternityNum)

local PortalModule = {}

PortalModule.Portals = {
	{
		PortalName = "OverworldPortal",
		ObbyName = "OverworldObby",
		ValueName = "Multiplier",
		RequiredValue = en.convert(2),
		Color = Color3.fromRGB(0, 174, 255),
		Rewards = {
			{Amount = en.convert(100), Chance = 50, Currency = "Cash"},
			{Amount = en.convert(200), Chance = 30, Currency = "Cash"},
			{Amount = en.convert(500), Chance = 20, Currency = "Cash"}
		}
	},
}

return PortalModule

this is my error:
image

still available if someone is down to help :+1:

at what line exactly?

print(player.Name .. " has been rewarded with " .. tostring(reward.Amount) .. " " .. reward.Currency) ?

also by the various amount of comment lines it seems you asked chat gpt to make the script for you, thats okay but i dont suggest using chat gpt for complex scripts cause the AI actually sucks at luau

i made alot of the script, but since i was getting the error i tried asking chatgpt, it tried fixing it but it didnt work and it added a bunch of comment lines, but the error is actually coming from this line:

aint gonna lie im kinda confused by the error as well, is the script using number values or string values?

also is reward.Amount the value or a string? maybe do reward.Amount.Value ?

I’m pretty sure the script uses string values, not number values since my leaderstat uses string values:

also, reward.Amount i think is the value, when i do reward.Amount.Value i get a different error:

I am so sorry I didn’t continue to assist you since you’re probably the most actively replying user I’ve come across in #help-and-feedback:scripting-support, but can you provide the “chooseReward” function?

Edit: Nevermind, I have taken note of its existence in the top of the script.

Ah wait I see.

You’re using a BigNum library, can you send the link to that source?

I need to quickly gloss over it because that’s gonna be your issue.

A part will tell when it touched a different part … as you have it set up. But, it will not keep telling it is touching unless it moves again. This happens to the player also. Walk on a touch part it will show touched. Stand there on it and it will not tell you’re still touching it. This is an optimization thing not a bug.

Script looks correct … didn’t go too deep however.
Check endPart is Anchored and set to CanCollide = true

I actually was not the one to implement it in my game, so i dont think i know the source, but if you search up eternitynum and check this out, this is the correct one :+1:
image

I’m not sure this is related, have you checked out the type of error that I am receiving?

So, that BigNum module does not support just doing a + b where a is a number and b is an “eternity” number

local en = require(workspace.EternityNum) -- Used this for simplicity sake (so I didnt go insane with red underlines due to errors)

local function rewardPlayer(player, reward)
	if reward then
		local currencyValue = player.Data.Stats:FindFirstChild(reward.Currency)
		if currencyValue and currencyValue:IsA("StringValue") then
			local Current = en.fromString(currencyValue.Value)
			if not Current then error("There was a silent error within the EternityNum module.", 2) end
			local Sum = en.add(Current, reward.Amount)
			currencyValue.Value = en.short(Sum) -- This is Short notation (1.5k, 15k, 97.99TOgCeMiDqgCe, etc...) (yes that's a real number)
		end
		print(player.Name .. " has been rewarded with " .. en.toString(reward.Amount) .. " " .. reward.Currency)
	end
end