Dozetopgg
(NotAngel)
August 6, 2024, 3:59pm
#1
Why, after receiving a level, the exp is reset and returns the next time you press it?
module:
local Player = game:GetService("Players").LocalPlayer
local LevelModule = {}
LevelModule.__index = LevelModule
function LevelModule.new()
local self = setmetatable({}, LevelModule)
self.Level = Player.other.Level.Value
self.Exp = Player.other.Exp.Value
self.Need = Player.other.Need.Value
return self
end
function LevelModule:IncreaceLevel()
if self.Exp >= self.Need then
self.Exp -= self.Need
self.Level += 1
self.Need *= 2
Player.other.Level.Value = self.Level
Player.other.Exp.Value = self.Exp
Player.other.Need.Value = self.Need
return self
end
end
return LevelModule
local script:
Main.Attack.Button.MouseButton1Click:Connect(function()
if not AttackCooldown then
AttackCooldown = true
RemoteEvents.Attack:FireServer()
RemoteEvents.AddCoins:FireServer()
RemoteEvents.AddExp:FireServer()
LevelModule.new():IncreaceLevel()
UpdateUI()
Main.Attack.Cooldown.Visible = true
task.wait(0.3)
Main.Attack.Cooldown.Visible = false
AttackCooldown = false
end
end)
script:
RemoteEvents.AddExp.OnServerEvent:Connect(function(player: Player)
player.other.Exp.Value += math.random(5, 15)
end)
1 Like
When you level up, don’t you change Player.other.Exp.Value only on the client?
1 Like
Dozetopgg
(NotAngel)
August 6, 2024, 4:18pm
#4
I change the value only in the module
1 Like
I understand, but you are not using the amount of exp collected from the module script in the server script, but the amount that is in value
1 Like
Dozetopgg
(NotAngel)
August 6, 2024, 4:21pm
#6
how can I update self.Exp when adding exp
1 Like
I think you can try this:
-- module script func.
function LevelModule:AddExp(amount)
self.Exp += amount
Player.other.Exp.Value = self.Exp
end
-- server script
RemoteEvents.AddExp.OnServerEvent:Connect(function(player: Player, levelModule)
levelModule:AddExp(math.random(5, 15))
end)
Dozetopgg
(NotAngel)
August 6, 2024, 4:27pm
#9
levelmodule comes out of nowhere
1 Like
Dozetopgg
(NotAngel)
August 6, 2024, 4:28pm
#10
what if I do this: RemoteEvents.AddExp:FireServer(LevelModule)
1 Like
Oh, exactly:
Main.Attack.Button.MouseButton1Click:Connect(function()
if not AttackCooldown then
AttackCooldown = true
RemoteEvents.Attack:FireServer()
RemoteEvents.AddCoins:FireServer()
RemoteEvents.AddExp:FireServer(LevelModule)
LevelModule.new():IncreaceLevel()
UpdateUI()
Main.Attack.Cooldown.Visible = true
task.wait(0.3)
Main.Attack.Cooldown.Visible = false
AttackCooldown = false
end
end)
1 Like
Dozetopgg
(NotAngel)
August 6, 2024, 4:29pm
#12
tables cannot be transferred
1 Like
Uh-huh, there are 2 options here:
You can rewrite the scripts so that the main table of the module is on the server, and then use the remote function to get all the data to client.
(less secure) you must use the remote function to take data from the client
1 Like
Dozetopgg
(NotAngel)
August 6, 2024, 4:33pm
#14
Do I need to transfer the modules somewhere? can be more I want to do 1 option
1 Like
You can place the module to the server script service or server storage.
1 Like
Dozetopgg
(NotAngel)
August 6, 2024, 4:42pm
#16
now error
local Services = require(game.ReplicatedStorage.Utils.Services).new():GetServices()
local VarialbesModule = {}
VarialbesModule.__index = VarialbesModule
function VarialbesModule.new()
local self = setmetatable({}, VarialbesModule)
self:InitializeVariables()
return self
end
function VarialbesModule:InitializeVariables()
self.Player = Services.Players.LocalPlayer
self.Modules = Services.ServerStorage.Modules
self.Utils = Services.ReplicatedStorage.Utils
self.RemoteEvents = Services.ReplicatedStorage.RemoteEvents
self.RemoteFunctions = Services.ReplicatedStorage.RemoteFunctions
end
function VarialbesModule:GetVariables()
return self
end
return VarialbesModule
1 Like
Module script function:
local transferDataEvent = game:GetService("ReplicatedStorage"):FindFirstChild("RemoteFunction")
function LevelModule:GetData()
local data = {
["Exp"] = self.Exp,
["Level"] = self.Level,
["Need"] = self.Need
}
return data
end
transferDataEvent.OnServerInvoke = function(...)
return LevelModule:GetData()
end
Local script:
local transferDataEvent = game:GetService("ReplicatedStorage"):FindFirstChild("RemoteFunction")
local data = transferDataEvent:InvokeServer()
1 Like
Dozetopgg
(NotAngel)
August 6, 2024, 4:44pm
#18
I tried to change the module to variables to get Modules from ServerStorage and got an error
1 Like
it just cant find Modules folder in server storage
Edit: You have this folder in server storage? You using .new() in server script?
1 Like
Dozetopgg
(NotAngel)
August 6, 2024, 4:49pm
#20
I literally tried to contact from the server module to the client
1 Like