Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Issue with event and DataStore2 ?

Hello everyone,

Im trying to use DataStore2, for that I created everything to store etc...

Then I want that when the player joins he receives his money but locally. So I used a OnClientEvent but is it for me impossible to get back the value of money. Here is my code, Server Sided :

local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")

local datastore2 = require(1936396537)

local defaultmoney = 50

players.PlayerAdded:Connect(function(player)
	local MoneyStore = datastore2("Money", player)
	
	local function updateClientMoney(amount)
		replicatedStorage.RemoteEvents.UpdateMoney:FireClient(player, amount)
	end
	
	updateClientMoney(MoneyStore:Get(defaultmoney))
	
	MoneyStore:OnUpdate(updateClientMoney)
end)

And here is my code , Client Sided:

local moneyevent = game:GetService("ReplicatedStorage").RemoteEvents.UpdateMoney
local money 

moneyevent.OnClientEvent:Connect(function(amount)
	print(amount)
	money = amount
end)

print(money)

But when I do that i got nil for print(money). To ensure that the event is correctly fired I put also a print(amount) which works.

I got another question which I still don't found the solution. Let's imagine that I have 10 part in workspace and that I want to make a variable shortcut for each of them, so we can do the following code :

local part1 = game.workspace.part1
local part2 = game.workspace.part2
local part3 = game.workspace.part3

and etc...

But I was wondering if we can do something like that :

for x = 1,11 do 
	local part"x" = game.workspace.part"x"

but of course that didn't work, so I was looking to know if is possible to "include" the "x" into this for loop to get the same things as writing all variable shortcut one by one ?

Thank's in advance. Have a nice day.

2 answers

The problem lies inside of your function call on line 15. The problem isn't with the remote events; it's a problem with the datastore module. I'm not familiar with how it works therefore I unfortunately can't help you with that.

Your code works fine if you do updateClientMoney(defaultmoney). It prints 50 on the client as expected.

The 2nd problem is quite simple.

local part1 = workspace:FindFirstChild("Part1")
local part2 = workspace:FindFirstChild("Part2")
local part3 = workspace:FindFirstChild("Part3")
-- ... and so on

This can be achieved by using the FindFirstChild function provided by Roblox and string concatenation.

for i = 0,10 do
	local part = workspace:FindFirstChild("Part" .. i)
end

Hello,

Thank you for your quick answer :D

First of all, I tried to put updateClientMoney(defaultmoney) and that doesn't work i still got a nil.

Concerning the DataStore2 system, the Get("a random number") function is used to set the default value of a datastore and that only if the value in it is equal to nil (as I understood).

The Get() only with nothing in, allow to get the value of the datastore. So when i do updateClientMoney(MoneyStore:Get(defaultmoney)) this script only activate when the player haven't data in is datastore.

The real problem concerning my first question is that he seems that i can't edit a value in a event function. Because when I print(amount) I got 50 but it's seem impossible to attribute this value to a variable defined before. It's like it's impossible to get out the value of an event function ?

For example If we try this code :

local function updateClientMoney(amount)
	replicatedStorage.RemoteEvents.UpdateMoney:FireClient(player, amount)
end

updateClientMoney(50)

and also :

moneyevent.OnClientEvent:Connect(function(amount)
	print(amount)
    money = amount
end)
print(money)

I still get nil.

Secondly, for the for loop that seems what i wanted but there is still a problem. If we remove the local to the part variable, and i do print(part) I got a nil. And also, my real goal of this for loop was to create for each part a specific variable that i can use later on. Than instead of writing one by one the path of each objects.

Anyway, a really big thanks again for your answer !

This discussion is preserved as it was. New answers, comments, and votes are closed.