Why is this part of my DataManager for my Module script working?
------ Game Functionality -------
local Dummy1 = workspace:WaitForChild("L1").Dummy
local FinishPad = workspace:WaitForChild("L1").FinishPart
FinishPad.Touched:Connect(function(hit)
if hit.Parent.Name == "Dummy" then
Profile.Dummy1Xp = Profile.Dummy1Xp + 1
end
end)
while true do
task.wait()
Dummy1:WaitForChild("Head").XPTagGui.TextLabel.Text = "XP: "..Profile.Dummy1Xp.."/10"
end
------- Value Caps -------
while true do
if Profile.Dummy1Xp ~= math.abs(0,10) then
Profile.Dummy1Xp = 10
end
end
Its not erroing at all, and Im confused.
The Full Script:
-- Profile table is what empty profiles will default to.
-- Updating the template will not include missing template values
-- in existing player profiles!
local Profile = {
Brickbux = 0,
Level = 1,
Dummy1Xp = 0
}
----- Loaded Modules -----
local ProfileService = require(game.ServerScriptService.ProfileService)
----- Private Variables -----
local Players = game:GetService("Players")
local ProfileStore = ProfileService.GetProfileStore(
"PlayerData",
Profile
)
local Profiles = {} -- [player] = profile
----- Private Functions -----
local function PlayerAdded(player)
local profile = ProfileStore:LoadProfileAsync("Player_" .. player.UserId)
if profile ~= nil then
profile:AddUserId(player.UserId) -- GDPR compliance
profile:Reconcile() -- Fill in missing variables from Profile (optional)
profile:ListenToRelease(function()
Profiles[player] = nil
-- The profile could've been loaded on another Roblox server:
player:Kick()
end)
if player:IsDescendantOf(Players) == true then
Profiles[player] = profile
-- A profile has been successfully loaded:
else
-- Player left before the profile loaded:
profile:Release()
end
else
-- The profile couldn't be loaded possibly due to other
-- Roblox servers trying to load this profile at the same time:
player:Kick()
end
end
----- Initialize -----
-- In case Players have joined the server earlier than this script ran:
for _, player in ipairs(Players:GetPlayers()) do
task.spawn(PlayerAdded, player)
end
----- Connections -----
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(function(player)
local profile = Profiles[player]
if profile ~= nil then
profile:Release()
end
end)
------ Game Functionality -------
local Dummy1 = workspace:WaitForChild("L1").Dummy
local FinishPad = workspace:WaitForChild("L1").FinishPart
FinishPad.Touched:Connect(function(hit)
if hit.Parent.Name == "Dummy" then
Profile.Dummy1Xp = Profile.Dummy1Xp + 1
end
end)
while true do
task.wait()
Dummy1:WaitForChild("Head").XPTagGui.TextLabel.Text = "XP: "..Profile.Dummy1Xp.."/10"
end
------- Value Caps -------
while true do
if Profile.Dummy1Xp ~= math.abs(0,10) then
Profile.Dummy1Xp = 10
end
end
-- this is the best way to get the player from the character, take notes
local function onTouch(hit)
local character = hit:FindFirstAncestorWhichIsA("Model")
if character then
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local player = Players:GetPlayerFromCharacter(character)
if player then
local profile = Profiles[player]
if profile then
profile.Data.Dummy1Xp = math.clamp(profile.Data.Dummy1Xp + 1, 0, 10)
Dummy1Xp:WaitForChild("Head").XPTagGui.TextLabel.Text = "XP: " .. profile.Data.Dummy1Xp .. " / 10"
end
end
end
end
end
ProfileService isn’t meant for NPCs usually, if that’s what you’re using it for that’s not the right way. You could make it that way, but the way you implemented it isn’t exactly right.
Also, you can’t run two while loops at the same time, the first one gets ran until it is ended. You may notice an issue with the second one overflowing.
Use task.spawn for multiple loops, and task.wait in loops
-- Profile table is what empty profiles will default to.
-- Updating the template will not include missing template values
-- in existing player profiles!
local Profile = {
Brickbux = 0,
Level = 1,
Dummy1Xp = 0
}
----- Loaded Modules -----
local ProfileService = require(game.ServerScriptService.ProfileService)
----- Private Variables -----
local Players = game:GetService("Players")
local ProfileStore = ProfileService.GetProfileStore(
"PlayerData",
Profile
)
local Profiles = {} -- [player] = profile
----- Private Functions -----
local function PlayerAdded(player)
local profile = ProfileStore:LoadProfileAsync("Player_" .. player.UserId)
if profile ~= nil then
profile:AddUserId(player.UserId) -- GDPR compliance
profile:Reconcile() -- Fill in missing variables from Profile (optional)
profile:ListenToRelease(function()
Profiles[player] = nil
-- The profile could've been loaded on another Roblox server:
player:Kick()
end)
if player:IsDescendantOf(Players) == true then
Profiles[player] = profile
-- A profile has been successfully loaded:
else
-- Player left before the profile loaded:
profile:Release()
end
else
-- The profile couldn't be loaded possibly due to other
-- Roblox servers trying to load this profile at the same time:
player:Kick()
end
end
----- Initialize -----
-- In case Players have joined the server earlier than this script ran:
for _, player in ipairs(Players:GetPlayers()) do
task.spawn(PlayerAdded, player)
end
----- Connections -----
Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(function(player)
local profile = Profiles[player]
if profile ~= nil then
profile:Release()
end
end)
------ Game Functionality -------
local Dummy1 = workspace:WaitForChild("L1").Dummy
local FinishPad = workspace:WaitForChild("L1").FinishPart
local function onTouch(hit)
local character = hit:FindFirstAncestorWhichIsA("Model")
if character then
local humanoid = character:FindFirstChildWhichIsA("Humanoid")
if humanoid then
local player = Players:GetPlayerFromCharacter(character)
if player then
local profile = Profiles[player]
if profile then
profile.Data.Dummy1Xp = math.clamp(profile.Data.Dummy1Xp + 1, 0, 10)
Dummy1:WaitForChild("Head").XPTagGui.TextLabel.Text = "XP: " .. profile.Data.Dummy1Xp .. " / 10"
end
end
end
end
end
------- Value Caps -------
while true do
if Profile.Dummy1Xp ~= math.abs(0,10) then
Profile.Dummy1Xp = 10
end
end