How would I change the data from outer scripts?

– SERVICES
local RS = game:GetService(“ReplicatedStorage”)
local PlrService = game:GetService(“Players”)
local DSS = game:GetService(“DataStoreService”)
local HTTPService = game:GetService(“HttpService”)

– Variables

local ClassDS = DSS:GetDataStore(“_PlayerClas”);

– Tables

local PlayerClasses = { – how would I change the stats from this table from outer scripts and modules?
– [PlayerName] = {};

}

local OnJoinTable = {
– true is if there is no data and you need to add a base data
[true] = function(player, result)
local PlayerClass = require(script.PlayerClass).new(player);
PlayerClasses[PlrService:GetUserIdFromNameAsync(player.Name)] = PlayerClass

	print(HTTPService:JSONEncode(PlayerClass))
	task.wait(0.3)
	PlayerClass:AddJujutsuTokens(50)
	print(HTTPService:JSONEncode(PlayerClass))
end,
[false] = function(player, result)
	local PlayerClass = require(script.PlayerClass).new(player, HTTPService:JSONDecode(result));
	PlayerClasses[PlrService:GetUserIdFromNameAsync(player.Name)] = PlayerClass
	
	print(HTTPService:JSONEncode(PlayerClass))
	task.wait(0.3)
	PlayerClass:AddJujutsuTokens(50)
	print(HTTPService:JSONEncode(PlayerClass))
end

}

– Functions

local function Encode(EnTable, bool)
local _Data = nil
if bool then
_Data = HTTPService:JSONEncode(EnTable)
else
_Data = HTTPService:JSONDecode(EnTable)
end
return _Data
end

local function Save(playerId, _PlayerClass)
local success, result = pcall(function()
local JSONStructure = HTTPService:JSONEncode(_PlayerClass)
ClassDS:SetAsync(playerId, JSONStructure)
end)
end

local function PlayerLeave(player)
local UserId = PlrService:GetUserIdFromNameAsync(player.Name)
Save(player, PlayerClasses[UserId])
end

local function PlayerJoined(Player)

local UserID = PlrService:GetUserIdFromNameAsync(Player.Name)

local success, result = pcall(function()
	return ClassDS:GetAsync(UserID)
end)

if not success then
	warn("UNABLE TO RETRIEVE" .. Player.Name .. " DATA! SUGGESTIVE ACTION : KICK USER TO PREVENT DATA LOSS DAMAGE")
end
OnJoinTable[result == nil or result == "null"](Player, result)

RS.Remotes.DataChangeRequest.OnInvoke(function()
	print("A")
end)

end

– Events

PlrService.PlayerAdded:Connect(PlayerJoined);
PlrService.PlayerRemoving:Connect(PlayerLeave);

game:BindToClose(function()
require(script.ForILoop).PairsForLoop(Save, PlayerClasses)
end)

1 Like

You can use a bindable event to let scripts trigger events and change values within other scripts.
An example of how to use bindable events looks like this:

The script listening for an event:

--Script 1 
local bindableEvent = --Location of your bindable event...

bindableEvent.Event:Connect(function(param1, param2) 
    --....
end)

The script firing the event:

--Script 2 
local bindableEvent = --Location of your bindable event...

bindableEvent:Fire(arg1, arg2)

You can pass as many arguments as you want through bindable events and set the values in your table with them. Just keep in mind that bindable events only work for Server script to Server script communication.

I love you pookie bear
:heartbeat::heartbeat::heartbeat::revolving_hearts::revolving_hearts::sparkling_heart::sparkling_heart::sparkling_heart::sparkling_heart::heart::heart::heart::heart_on_fire::heart_on_fire::heart_on_fire::heart_on_fire::heart_on_fire::heart_on_fire::heart_on_fire::gift_heart::gift_heart::gift_heart::gift_heart::love_letter::love_letter::love_letter::love_letter::love_letter::heart_eyes::heart_eyes::heart_eyes::heart_eyes::smiling_face_with_three_hearts::smiling_face_with_three_hearts::smiling_face_with_three_hearts::smiling_face_with_three_hearts::smiling_face_with_three_hearts::smiling_face_with_three_hearts::smiling_face_with_three_hearts::smiling_face_with_three_hearts::heart_hands::heart_hands::heart_hands::heart_hands::heart_hands::heart_hands:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.