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

Getting and receiving DataStore with RemoveFunctions?

So I am trying to invoke the server which works fine:

-- LocalScript
local points = dataGetEvent:InvokeServer("points");

in my server side script, how would I return the player points back to the local script so it would set the variable 'points' to the player points? I know I can do:

function dataGetEvent.OnServerInvoke(player, item)
	if (item == "points") then
		-- return back to localscript?
	end
end

I am wanting to return it back straight away so I can use the assigned variable in the localscript on the next line.

1 answer

You can use the keyword returnto return the points to the localscript.

So, it can be:

local DataStorePoints = game:GetService("DataStoreService"):GetDataStore("Points")

function dataGetEvent.OnServerInvoke(player, item)
    if (item == "points") then
       return DataStorePoints:GetAsync("user_"..tostring(player.UserId)..", points) or nil
    end
end

In the localscript, the player will ask for points, the function dataGetEvent will be fired with the arguments: maquilaque (example), points

If the item is the points, then it will return from the DataStore Service, called from the :GetService() method,* a variable*.

From DataStoreService, we get the store "Points", and to get the player's stuff, we use a key. In this example, the key is user_"..tostring(player.UserId)", so mine could be "user_25776731" because my Id is 25776731.

Then we call a scope to get the points, in this case, it's "points".

All this stuff will be returned with the keyword return, but if doesn't exist, then it will return nil.

Hope it will work!

Don't forget to accept my answer if it works!

3 comments

0

return is not a function, it's a keyword.

0

Ok thanks I'll correct it!

0

Thanks, and you also don't need the tostring

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