Here is my code within a server script for adding experience to the player in a certain stat.
The first print functions that print the values of the relevant variables and calculations in the if checks do print, so the code is getting at least that far.
However, neither of the print functions within both of the if checks are printing. According to the prints from the calculations, the second if statement should be getting through but isn’t for some reason.
local statsEvent = game.ReplicatedStorage.RemoteEvents:WaitForChild("StatsEvent")
local expEvent = game.ReplicatedStorage.RemoteEvents:WaitForChild("ExpEvent")
local serverScriptService = game:GetService("ServerScriptService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local dataManager = require(serverScriptService.Modules.DataManager)
expEvent.OnServerEvent:Connect(function(plr, eventtype, xpType, lvlType, amount)
local profile = dataManager.Profiles[plr]
local character = plr.Character
if eventtype == "addExp" then
local expType = tostring(xpType) -- e.g. StrengthExp
local expTypeStat = expType:gsub("Exp", "") -- e.g. Strength
local profileDataExpType = rawget(profile.Data, expTypeStat) -- e.g. profile.data.Strength.Value
local profileDataExp = rawget(profile.Data, expType) -- e.g. profile.data.StrengthExp.Value
print("Exp needed: " .. tostring(100 * ((profileDataExpType + 1) / 2))) -- prints out 50
print("Current Exp: " .. tostring(profileDataExp)) -- prints out 60
if profileDataExp <= math.floor(100 * ((profileDataExpType + 1) / 2)) then
print("1") --doesn't print
profileDataExp = profileDataExp + (amount * (1 + (profile.Data.Fate.Value / 100)))
plr.leaderstats:FindFirstChild(expType).Value = profileDataExp
expEvent:FireClient(plr, "updateExp")
if profileDataExp >= math.floor(100 * ((profileDataExpType + 1) / 2)) then
print("2") -- doesn't print
local leftoverExp = profileDataExpType - math.round(100 * (profileDataExpType / 2))
profileDataExpType = profileDataExpType + 1
plr.leaderstats:FindFirstChild(expTypeStat).Value = profileDataExpType
profileDataExp = 0 + leftoverExp
plr.leaderstats:FindFirstChild(expType).Value = profileDataExp
expEvent:FireClient(plr, "updateExp")
end
end
end
end)
The code doesn’t throw up any errors and seems to work as intended up until the if checks past the eventtype if check.
Lukas Raby is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.