Whats wrong with this script?

Im trying to use datastore2 on local script and so im trying to returning information but this happens why?

local communication = game.ReplicatedStorage:WaitForChild("Events").Communication


communication.OnServerInvoke:Connect(function()
	return local DataStore2 = require(1936396537)
end)

1 Like

What exactly are you trying to do?

return is used to return stuff, but
local is used to declare variable.

1 Like

im trying to return local datastore2 because i can’t get that on client

But DataStore2 is a module, there is no data in it.

Are you looking to return data associated with that player?

local communication = game.ReplicatedStorage:WaitForChild("Events").Communication


communication.OnServerInvoke:Connect(function()
	return require(1936396537)
end)
1 Like

You have to put return after your line of code

local communication = game.ReplicatedStorage:WaitForChild("Events").Communication


communication.OnServerInvoke:Connect(function()
	local DataStore2 = require(1936396537)
return
end

Maybe something like that

Your problem is that you have a local in there, which can only be used to declare variables. But that won’t fix it. Data stores can’t be used in a local script, and you can’t send functions to the client with a remote function anyways, so this whole thing is pointless. Even if it did work, it would open you up to all kinds of easy hacking. Data stores need to be handled by the server.

First, this won’t work regardless of what you do, as DataStore2 uses datastores, which can ONLY be handled by the server. If you’re trying to read specific information about a player, then do that on the server. You are returning the loaded module to the client, even though its supposed to be run on the server, which I don’t get why you would need to anyways.

2 Likes

oh, how would i make an exp system then if i can’t use d2 for it?

You can use d2, but it needs to be in a regular Script, not a local script. Data that you don’t want hacked should always be handled on the server like this.

but then how would i show everything on a gui?

Do i make a new datastore in a different script?

You can use DS2 for the SAVING data aspect of it.

For the actual system however, please open a new thread and clearly state what you do not understand or have trouble dealing with.

1 Like

Sorry, I was on mobile. This is how it should be.

Data is managed by the server. When a player makes a kill, the server deals the damage anyways and will know who made a kill. This way it is very difficult to hack the exp. Add the exp on the server, calculate level ups, save the data, and send the data to the LocalScript with a RemoteEvent so the data can be displayed.

Try not adding local after return .

but i have a updateexp function would i just delete it? And a updatelevel

OnServerInvoke uses a function callback, so trying to connect to it won’t work.

What is a callback? It’s a write only function that the game engine will run when required.

I would recommend declaring the function earlier in your code for better readability but this works fine as well. But as @JarodOfOrbiter mentioned trying to have the client manage data won’t work because clients don’t have access to datastores.

communication.OnServerInvoke = function()
    local DataStore2 = require(1936396537)
    return DataStore2
end
local communication = game.ReplicatedStorage:WaitForChild("Events").Communication


communication.OnServerInvoke = function()
	return require(1936396537)
end

Hello friend, the issue is that you returning “local”

  • The listed code below should work!
local communication = game.ReplicatedStorage:WaitForChild("Events").Communication

communication.OnServerInvoke:Connect(function()
 local DataStore2 = require(1936396537)
	return DataStore2 
end)

You can’t :Connect() on OnServerInvoke

local communication = game.ReplicatedStorage:WaitForChild("Events").Communication

communication.OnServerInvoke = (function() 
        return function()
             local DataStore2 = require(1936396537)
	     return DataStore2 
        end
 end)()

(yes I know this is wordy, I was bored)