Weird issue with tables

In my game I am trying to save the values from a module script but I run into a weird problem.

My module is inside a model, after printing the contents during runtime, the value I’m saving appears as true, when I leave the game and the contents is printed from my saving script, it is equal to false although nothing is changing it.

Runtime (using command bar) :
image
Saving script:
image

I have made sure to check that it is printing the same table and it is.

2 Likes

I think there’s another script that’s tampering/changing the data. Can you please show us the full script/contexts so we can understand the problem better to find the root cause?

1 Like

Its a new project, nothing is tampering with it, I even made sure to look through every line of code in the game.
When I join or leave it is true, during run time it is false.

Here is the function printing the table when I leave (relevant parts):

local function toTable(player)
	local data = {}
	for _,pet in pairs(player:WaitForChild("Pets"):GetChildren()) do
		local info = require(pet.Pet)
		local instance = {}
		print(info)
		instance[1] = info.Type
		instance[2] = info.Equipped
		instance[3] = info.Enchants
		instance[4] = info.Powers
		instance[5] = info.GivenName
		table.insert(data,instance)
	end
	
	return data
end

players.PlayerRemoving:Connect(function(player)
	local saving = toTable(player)
	local key = player.UserId.."-key"
	
	local succ,err = pcall(function()
		print(saving)
		petData:SetAsync(key,saving)
	end)
end)

part that handles getting data when you load into game:

players.PlayerAdded:Connect(function(player)
	local key = player.UserId.."-key"
	local saves = petData:GetAsync(key)
	
	local inv = Instance.new("Folder")
	inv.Name = "Pets"
	inv.Parent = player

	if saves then
		print(saves)
		for _,save in saves do
			local pet = pets:FindFirstChild(save[1])
			local equipped = save[2]
			local enchants = save[3]
			local extra = save[4]
			local name = save[5]
			
			if pet then
				createPet(pet,name,extra,equipped,enchants,inv)
			end
		end
	else
		print("New player!")
	end
end)
1 Like

The loading and saving data code looks fine to me. I’m thinking that it could be the way on how you set the values in the module script.

How do you create pets here? Do you just require the module script, and then get the index you want and set it to its respective value?

1 Like

Oh ye, sorry:

local function createPet(pet,name,extra,equipped,enchants,inv)
	local clonedInstance = pet:Clone()
	local info = require(clonedInstance.Pet)
	print(name,extra,equipped,enchants)
	info:SetValue('Equipped',equipped)
	info:SetValue('Enchants',enchants)
	info:SetValue('Powers',extra)
	info:SetValue('GivenName',name)
	
	clonedInstance.Name = getName(inv,clonedInstance.Name)
	clonedInstance.Parent = inv
end

module setvalue function:

function pet:SetValue(key,val,key2)
	if not runService:IsServer() then return end
	
	if not key2 and val ~= pet[key] then
		pet[key] = val
		fire(key,val)
	elseif val ~= pet[key][key2] then
		pet[key][key2] = val
		fire(key,val,key2)
	end
end
1 Like

Are you using OOP to create your pet system? Because I believe you are doing it wrong. It seems like you create a module script for every pet to create its functions etc… You should be only using one module script that handles everything.

1 Like

No, just using a module script to hold the values so its stored inside the pet model

I see, so you are using a module script to store a pet’s info for every pet created in the server…

Why can’t you use attributes? Because if, a server attempts to set a different value for a different key in a module script particularly, the new change won’t be replicated to the client. This means if you set a property of the module script to something else, only the server side is able to observe such change, while the client does not.

How do you print the values of the properties during runtime?

1 Like

Each pet has its own unique values which include tables aswell which crossed out attributes, I can’t use OOP since each pet starts with unique values and I have already handled the client replicating.

here is how I print it:

print(require(game.Players.ItzBloxyDev.Pets.Red.Pet))
1 Like

I see, let me try to replicate your code in studio and I’ll let you know if I find any problems. Sorry if this is going to take some time.

1 Like

Hey I am back, I found no issues using your method of printing the value from the module script. Read my script:

game.Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder")
	folder.Parent = player
	
	script.ModuleScript:Clone().Parent = folder
end)

task.wait(5)
print("value changed")
local module = require(game.Players:GetPlayers()[1].Folder.ModuleScript)
module.set(true)
print(module.value) -- prints true on the server, but FALSE on the client.

Module script

local module = {}

module.value = false

module.set = function(newVal)
	module.value = newVal
end

return module

Is this how you set the values? I tried to use your concept of loading and printing the value.

1 Like

What do you mean by runtime? As in, did you print the values from the module script when you join the server?

1 Like

Ye, using the command bar. char limit

1 Like

Have you confirmed that the data is actually saving?

1 Like

Aha! We found the issue. I got false when I tried to print the value.

So this is the problem. Even if you set the new value on the server, the command bar receives it differently.

But why are you concerned with the value being false on the command bar? I don’t think you are going to use it for any important scripting purposes, but now my point here is, the contents of a module script is different on the server, client and the command bar, so if any one of these “people” changed the value, it will NOT REPLICATE to other people.

1 Like

Ill test this by printing it from a script.

1 Like

Yes, it saves correctly. char limittt

1 Like

If you print it on a server script, it would print true because the change was made by the server, so only that change is visible to the server itself. If you print it in a command bar, then it prints a different value.

1 Like

this does seem to be the case (not sure why this happens). Thanks for the help. Ill mark that as solution.

1 Like

I believe Roblox does not allow this to happen is because of security reasons, hence they will have to create separate “invisible” module scripts on different perspectives.

1 Like