I was trying to make a script that saves when a player is in a way “lucky” and doesn’t get kicked out of my game first thing (lol) I don’t want to load data again, I just want to have it save once. My plan was to then look at the list of names of people who have completed it and give them a shoutout as a group shout. So anyway, here’s my script that makes it so you have a 1 in a million chance of getting into the game I’m the only exception, with a 100% chance to not be kicked. Can somebody tell me why this script is not printing anything in the output? You can test it yourself here or just look at the code and explain what I’ve done wrong to make it not print anything.
The Script
game.Players.PlayerAdded:Connect(function()
local gotLucky = false
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("SendInfo")
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)
gotLucky = true
local success, errorMessage = pcall(function()
myDataStore:SetAsync(player.UserId,gotLucky)
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)
end)
//Server Script
local Player = game.Players.LocalPlayer
print(Player) //nil
//LocalScript
local Player = game.Players.LocalPlayer
print(Player) //Gogols_220106
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
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)
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.
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()