Variable turns to 0 on client

Hello, Im trying to make a rebirth system and I need to save Rebirth Multiplier and Rebirth cost, but when I print Rebirth cost on server, its fine, but on client it turns 0.

Also prints that:
image

-- Local
local Rebirths = Leaderstats.Rebirths
local Multiplier = Player:WaitForChild('PlayerValues'):WaitForChild('Multiplier').Value

local PlayerValues = Player:WaitForChild('PlayerValues')
local RebirthCost = PlayerValues:WaitForChild('RebirthCost').Value

RebirthsPrompt.Triggered:Connect(function()
	RebirthsFrame.Visible = not RebirthsFrame.Visible
	
	RebirthsFrame.RebirthsSection.PriceValue.Text = RebirthCost..' đź‘…' 
	RebirthsFrame.RebirthsSection.Multiplier.Text = (Multiplier)..' x'
end)

BuyButton.MouseButton1Click:Connect(function()
	
	if Freakiness.Value >= RebirthCost then
		
		RebirthCost *= 2
		Multiplier += 0.5
		
		RebirthsFrame.Visible = false
		
		RebirthEvent:FireServer(RebirthCost, Multiplier)
		
	end
	
end)

-- Server
local PlayerValues = Instance.new('Folder', Player)
	PlayerValues.Name = 'PlayerValues'
	
	local Multiplier = Instance.new('IntValue', PlayerValues)
	Multiplier.Name = 'Multiplier'
	Multiplier.Value = 1
	
	local RebirthCost =  Instance.new('IntValue', PlayerValues)
	RebirthCost.Name = 'RebirthCost'
	RebirthCost.Value = 3

RebirthEvent.OnServerEvent:Connect(function(Player, RebSave, MultSave)

		local leaderstats = Player:WaitForChild('leaderstats')
		local Multiplier = Player.PlayerValues:WaitForChild('Multiplier')
		local RebirthCost = Player.PlayerValues:WaitForChild('RebirthCost')

		leaderstats.Freakiness.Value = 0
		leaderstats.Rebirths.Value += 1

		Multiplier.Value = MultSave
		RebirthCost.Value = RebSave

	end)

local DataToSave = DS:GetAsync(Player.UserId)
	
	if DataToSave then
		Freakiness.Value = DataToSave[1]
		Variables from 2 to 9
		Rebirths.Value = DataToSave[10]
		Multiplier.Value = DataToSave[11]
		RebirthCost.Value = DataToSave[12]
		
	else
		print('No data')
		
		local ValuesToSave = {
			Freakiness.Value,
			
			Rebirths.Value,
			Multiplier.Value,
			RebirthCost.Value
		}
		
		DS:GetAsync(Player.UserId, ValuesToSave)
		
		print('data created')
		
	end
	
end)

Players.PlayerRemoving:Connect(function(Player)
	
	local Leaderstats = Player:WaitForChild('leaderstats')
	
	local Freakiness = Leaderstats:WaitForChild('Freakiness')
	local Rebirths = Leaderstats:WaitForChild('Rebirths')
	local Multiplier = Player.PlayerValues:WaitForChild('Multiplier')
	local RebirthCost = Player.PlayerValues:WaitForChild('RebirthCost')
	
	local ValuesToSave = {
		Freakiness.Value,

		Player.UpgradeStatus:FindFirstChild('Freaky cat').Value,
		Player.UpgradeStatus:FindFirstChild('Freaky Bob').Value,
		Player.UpgradeStatus:FindFirstChild('Alien freak').Value,
		Player.UpgradeStatus:FindFirstChild('Eater').Value,
		Player.UpgradeStatus:FindFirstChild('Freaky Freddy').Value,
		Player.UpgradeStatus:FindFirstChild('Quincy freak').Value,
		Player.UpgradeStatus:FindFirstChild('Mahito freak').Value,
		Player.UpgradeStatus:FindFirstChild('Extra freaky Bob').Value,
		
		Multiplier.Value,
		RebirthCost.Value
	}
	
	DS:SetAsync(Player.UserId, ValuesToSave)
	
end)



1 Like

What line is that error printing for?

This one. also ill try to replace emote event to emote function

Do you want to multiply the value per 2?

yeah, I need to to that and past this value into a players gui

if the multiplication is wrong try this

RebirthCost = RebirthCost * 2

Don’t do this. You’re storing the value of the ValueBase at the time the script starts. You have to just store the Instance and fetch the Value later on.

local Multiplier = Player:WaitForChild('PlayerValues'):WaitForChild('Multiplier')
local RebirthCost = PlayerValues:WaitForChild('RebirthCost')
---...
BuyButton.MouseButton1Click:Connect(function()
	
	if Freakiness.Value >= RebirthCost then
		
		RebirthCost.Value *= 2 -- update the value here
		Multiplier.Value += 0.5
		
		RebirthsFrame.Visible = false
		
		RebirthEvent:FireServer(RebirthCost.Value, Multiplier.Value)
		
	end
	
end)

Also, your remotes aren’t secure

its my first multiplayer project, so I dont know how to protect them

The main practice for securing remotes is to make checks in your server code to make sure that it makes sense to be receiving the remote. For example, if the rebirth event is fired, you should first write a check to make sure the player is at max level or whatever requirement you have.

Another good practice is to think about your code as you go along. Stop and think “how can an exploiter take advantage of this?” And whatever comes to mind, just implement checks and countermeasures in your code to make that impossible.

Edit: That’s pretty much the best I can think of. Someone please let me know if there are additional or better steps to take.

1 Like