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)