Trying to save if a player was lucky

If you are in a local script, you can’t use dataStore.
If you are in script, you can’t use localplayer.

@Gogols_220106
This video is using a server script for datastore:

Also, I’ve used local player in server scripts before. Can you give proof about either of those claims?

Correct me if I’m wrong. But using Local Player on the server side will return nil.

local Player = game.Players.LocalPlayer
print(Player)

if you use this on a local script it will print the players name however if you use this on a server script in will print nil.

So that video’s outdated now? Should I just fire a remote event to do that?

@Gogols_220106 I don’t know why I thought that would work thanks for helping tho

1 Like

@mouseRBM, yes you are correct

//Server Script
local Player = game.Players.LocalPlayer
print(Player)  //nil

//LocalScript
local Player = game.Players.LocalPlayer
print(Player) //Gogols_220106
1 Like

Re-write the script from stratch without pasting anything then it will work. You basically made a player added event in a player added event, also the gotlucky variable is useless just use true instead of it

I would rather just delete the parts I don’t need. After getting rid of the playeradded events and the DataStore things, it’s back to the original

Script:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("SendInfo")
local player = Players.LocalPlayer

local function OnPlayerAdded(player)
	local random = math.random(1,1000000)
	if player.UserId == 1463962029 then
		random = 53482
	end
	if random == 53482 then
		local randomEarnings = math.random(1,5)
		print(player.Name .. " HAS GOTTEN EXTREMELY LUCKY! YAY!")
		remoteEvent:FireClient(player,randomEarnings)
	else
		player:Kick("Unlucky!")
	end
end
Players.PlayerAdded:Connect(OnPlayerAdded)

I don’t really understand what you mean by that.
EDIT: nevermind I see what you mean

now you removed 2 playeradded events you just had to remove 1

Yeah editing it I noticed that before you said it lol
Edited it and it works how it originally worked before I messed with it

I’ll reply again if something isn’t working on the client side.

1 Like

@Gogols_220106 @takticiadam @mouseRBM
Alright so it’s printing success but how would I actually see it with the DataStore editor plugin?

I guess check out the version that you can copy to see what I mean

I don’t use many plugins, just for construction, I know the name of the plugin but I don’t know how to use it. :frowning:

Okay I’m posting a video of it maybe you can see then I don’t know there’s other people who can hopefully help me if you can’t

I don’t believe you can post data to the api from the client, at least conventionally as yours script shows

You can’t. I messed up and it’s fixed. This is the server script that works

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("SendInfo")

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

local function OnPlayerAdded(player)
	local random = math.random(1,1000000)
	if player.UserId == 1463962029 then
		random = 53482
	end
	if random == 53482 then
		local randomEarnings = math.random(1,5)
		print(player.Name .. " HAS GOTTEN EXTREMELY LUCKY! YAY!")
		remoteEvent:FireClient(player,randomEarnings)
		local success, errorMessage = pcall(function()
			myDataStore:SetAsync(player.UserId,randomEarnings)
		end)
		if success then
			print(player.Name .. "'s data has been saved because they got lucky!")
		else
			print(player.Name .. "'s data did not save!")
			warn(errorMessage)
		end
	else
		player:Kick("Unlucky!")
	end
end
Players.PlayerAdded:Connect(OnPlayerAdded)

Datastore editor plugin doesnt use a foreign language that you cant understand, just open it and do what it says then youll find it.

@takticiadam Yeah but it isn’t showing up:

You better contact who made the plugin then, I see no problem on your side

In the key box, you have to enter the key you used to save your data. I read your script and it says you used player.UserId. To find your data, you have to enter your user id into the key

Based on what I’ve read, the issue right now is that you don’t know who’s the lucky user, especially their playerId. Using a normal DataStore, you’d have to provide a key to view their values.

How would you do so since you don’t know their ID? You simply can’t.

An alternative is to use OrderedDataStore, or even just a simple webhook to your discord server.

OrderedDataStore:

Scroll to the last of this page to read regarding OrderedDataStore, Data Stores | Documentation - Roblox Creator Hub

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetOrderedDataStore("myDataStore")

local function showLuckyPeople()
    local isAscending = false
    local pageSize = 10
    local pages = myDataStore:GetSortedAsync(isAscending, pageSize)
    local topTen = pages:GetCurrentPage()

    -- the data in 'topTen' is stored with the index being the index on the page
    -- for each item, 'data.key' is the key in the OrderedDataStore and 'data.value' is the value
    for rank, data in ipairs(topTen) do
        local name = data.key
        local points = data.value
        print(data.key .. " is lucky: " .. data.value)
    end

    -- potentially load the next page... (?)
    --pages:AdvanceToNextPageAsync()
end

showLuckyPeople()

Webhook:

Read here on how to set up a webhook: Discord Integration: A guide on using Discord through Roblox [UPDATED]

local HTTPService = game:GetService("HttpService")
local url = "webhook url here"

-- if random == 53482

	local Data = {
		["username"] = "Lucky!";
		["content"] = player.Name .. " -" .. player.UserId .. " HAS GOTTEN EXTREMELY LUCKY! YAY!";
	}
	Data = HTTPService:JSONEncode(Data)
	HTTPService:PostAsync(url, Data)

@random_player201 Thanks, I have already done that. Everything works fine now! I should have said that everything was working