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)
local communication = game.ReplicatedStorage:WaitForChild("Events").Communication
communication.OnServerInvoke:Connect(function()
return require(1936396537)
end)
local communication = game.ReplicatedStorage:WaitForChild("Events").Communication
communication.OnServerInvoke:Connect(function()
local DataStore2 = require(1936396537)
return
end
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.
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.
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.
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)
local communication = game.ReplicatedStorage:WaitForChild("Events").Communication
communication.OnServerInvoke = (function()
return function()
local DataStore2 = require(1936396537)
return DataStore2
end
end)()