DataStore not running - lua

I'm trying to make a gamepass script where when you join you will be told to buy a gamepass. but When you join the game the Gamepass promt wont show up. and the output does not give me a error
local marketplaceservice = game:GetService("MarketplaceService")
local gamepassid = 11585012233
local function PromtPurchase()
local players = game.Players.LocalPlayer
local haspass = false
local success, message = pcall(function()
local hasspass =
marketplaceservice:PromptGamePassPurchase(players.UserId,gamepassid)
if haspass == true then
print("Player has pass")
marketplaceservice:PromptGamePassPurchase(players, gamepassid)
else
marketplaceservice:PromptGamePassPurchase(players, gamepassid)
end
end)
wait(1)
promtPurchase()

local hasspass = marketplaceservice:PromptGamePassPurchase(players.UserId,gamepassid)
This is a prompting function, so it'll just prompt every time, you want to be using
local hasspass = marketplaceservice:UserOwnsGamePassAsync(players.UserId, gamepassid)

Related

How do I make a leader board that saves and counts every single kill? (I'm just starting out by the way!) [duplicate]

I want to make a save system so that people don't have to restart every single time they play
I don't really know what to do so I will show you the code for my leader stats this is located in the work space
local function onPlayerJoin(player)
local leaderstats = Instance.new("Model")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local gold = Instance.new("IntValue")
gold.Name = "JumpBoost"
gold.Value = 150
gold.Parent = leaderstats
local speed = Instance.new("IntValue")
speed.Name = "Speed"
speed.Value = 20
speed.Parent = leaderstats
local coin = Instance.new("IntValue")
coin.Name = "CloudCoins"
coin.Value = 0
coin.Parent = leaderstats
local rebirths = Instance.new("IntValue")
rebirths.Name = "Rebirths"
rebirths.Value = 0
rebirths.Parent = leaderstats
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
Again I don't really know what to do so, please help.
The documentation for Data Stores is pretty good. An important warning for testing :
DataStoreService cannot be used in Studio if a game is not configured to allow access to API services.
So you will have to publish the game and configure it online to allow you to make HTTP requests and access the Data Store APIs. So be sure to look at the section in that link titled, Using Data Stores in Studio, it will walk you through the menus.
Anyways, right now, you are creating the player's starting values when they join the game. DataStores allow you save the values from the last session and then load those in the next time they join.
Roblox DataStores allow you to store key-value tables. Let's make a helper object for managing the loading and saving of data.
Make a ModuleScript called PlayerDataStore :
-- Make a database called PlayerExperience, we will store all of our data here
local DataStoreService = game:GetService("DataStoreService")
local playerStore = DataStoreService:GetDataStore("PlayerExperience")
local PlayerDataStore = {}
function PlayerDataStore.getDataForPlayer(player, defaultData)
-- attempt to get the data for a player
local playerData
local success, err = pcall(function()
playerData = playerStore:GetAsync(player.UserId)
end)
-- if it fails, there are two possibilities:
-- a) the player has never played before
-- b) the network request failed for some reason
-- either way, give them the default data
if not success or not playerData then
print("Failed to fetch data for ", player.Name, " with error ", err)
playerData = defaultData
else
print("Found data : ", playerData)
end
-- give the data back to the caller
return playerData
end
function PlayerDataStore.saveDataForPlayer(player, saveData)
-- since this call is asyncronous, it's possible that it could fail, so pcall it
local success, err = pcall(function()
-- use the player's UserId as the key to store data
playerStore:SetAsync(player.UserId, saveData)
end)
if not success then
print("Something went wrong, losing player data...")
print(err)
end
end
return PlayerDataStore
Now we can use this module to handle all of our loading and saving.
At the end of the day, your player join code will look very similar to your example, it will just try to first load the data. It is also important to listen for when the player leaves, so you can save their data for next time.
In a Script next to PlayerDataStore :
-- load in the PlayerDataStore module
local playerDataStore = require(script.Parent.PlayerDataStore)
local function onPlayerJoin(player)
-- get the player's information from the data store,
-- and use it to initialize the leaderstats
local defaultData = {
gold = 150,
speed = 0,
coins = 0,
rebirths = 0,
}
local loadedData = playerDataStore.getDataForPlayer(player, defaultData)
-- make the leaderboard
local leaderstats = Instance.new("Model")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local gold = Instance.new("IntValue")
gold.Name = "JumpBoost"
gold.Value = loadedData.gold
gold.Parent = leaderstats
local speed = Instance.new("IntValue")
speed.Name = "Speed"
speed.Value = loadedData.speed
speed.Parent = leaderstats
local coin = Instance.new("IntValue")
coin.Name = "CloudCoins"
coin.Value = loadedData.coins
coin.Parent = leaderstats
local rebirths = Instance.new("IntValue")
rebirths.Name = "Rebirths"
rebirths.Value = loadedData.rebirths
rebirths.Parent = leaderstats
end
local function onPlayerExit(player)
-- when a player leaves, save their data
local playerStats = player:FindFirstChild("leaderstats")
local saveData = {
gold = playerStats.JumpBoost.Value,
speed = playerStats.Speed.Value,
coins = playerStats.CloudCoins.Value,
rebirths = playerStats.Rebirths.Value,
}
playerDataStore.saveDataForPlayer(player, saveData)
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
Hope this helps!

Why is 'player' returning nil?

It keeps returning nil for player and saying that im trying to index a nil with 'WaitForChild' even though I have tried adding a wait command and changing player to 'game.Players.LocalPlayer' I'm new to scripting and I don't know what else to do.
local buyButton = script.Parent
local player = game:GetService("Players").LocalPlayer
local multiplier = player:WaitForChild("Multiplier")
buyButton.MouseButton1Up:Connect(function()
if multiplier.Value == 0 then
multiplier.Value = 1
end
end)
Instead of local player = game:GetService("Players").LocalPlayer you want to do:
local players = game:GetService("Players")
local player = players.localplayer

"Unable to cast value to Object" error message

So I am using a remote function, as seen at the end, and for some reason, a normal assignment of a variable won't work, it is giving me the error message, "Unable to cast value to Object," what is wrong?
local storeEvent = script.Parent.Parent.OpenStore
local slotNum = 1
script.Parent.Touched:Connect(function (hit)
if game.Players:GetPlayerFromCharacter(hit.Parent) then
storeEvent:InvokeClient(slotNum)
end
end)
and connecting to the other script:
script.Parent.OnInvoke:Connect(function (slot)
local StoreArrows = game.ReplicatedStorage.StoreArrows
StoreArrows.SlotNum.Value = slot
local cam = game.Workspace.Camera
local storeButtons = script.Parent
local camNum = game.ReplicatedStorage.StoreArrows.CamNum.Value
local camNumInst = game.Workspace.CamStorage:WaitForChild("Cam-"..camNum)
cam.CameraType = Enum.CameraType.Scriptable
cam.CFrame = camNumInst.CFrame
local clonedStoreButtons = StoreArrows:Clone()
clonedStoreButtons.Parent = player.PlayerGui.ScreenGui
end)
Keep in mind that many clients connect to a server at a time. So when you call a RemoteEvent's InvokeClient function, you have to tell it which client to invoke it on. The first parameter to InvokeClient is supposed to be the player, that's why the error is telling you that it cannot cast the slotNum value to a player object.
local storeEvent = script.Parent.Parent.OpenStore
local slotNum = 1
script.Parent.Touched:Connect(function(hit)
-- check that the thing that we touched is actually a player
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
-- tell that player to open the store
storeEvent:InvokeClient(player, slotNum)
end
end)

im making a roblox game and need to update the leaderboard stats when you click the baseplate

this is my code at the moment:
local players = game:WaitForChild("Players")
local function createLeaderboard(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
local baseclicks = Instance.new("IntValue", stats)
baseclicks.Name = "baseclicks"
stats.Parent = player
baseclicks.Value = 100
end
players.PlayerAdded:connect(createLeaderboard)
Im not sure if i need a clickdetector with a script inside or??
i dont know, please help.
If you don't want ClickDetectors on the Baseplate, simply, you could use mouse.Target. For example:
-- serverscript
local players = game:GetService("Players");
local function createLeaderboard(player);
local stats = Instance.new("Folder", player);
stats.Name = "leaderstats";
local baseclicks = Instance.new('IntValue', stats);
baseclicks.Name = 'baseclicks'
baseclicks.Value = 100;
end
-- localscript in startercharacterscript
local players = game:GetService("Players");
local client = players.LocalPlayer;
local mouse = client:GetMouse(); --method for getting the client's mouse
local event = game.ReplicatedStorage.OnClick --our remote event
local leaderstats = client.leaderstats or client:WaitForChild("leaderstats");
local clickvalue = leaderstats.baseclicks or leaderstats:WaitForChild("baseclicks");
mouse.Button1Down:Connect(function()
if not (mouse.Target) then return; end
if (mouse.Target.Name == "Baseplate") then
event:FireServer(clickvalue.Value + 1); --fire the remote event
end
end
end)
-- server script in serverscriptservice for remote event receiver
game.ReplicatedStorage.OnClick.OnServerEvent:Connect(function(Player, Value)
Player.leaderstats.baseclicks.Value = Value;
end
However, when it comes to the game having multiple players clicking at once, remote events are not recommended as they are more than likely going to lag the server or cause network traffic - you're better off using ClickDetectors.

Leaderstats not working? or just not detecting the click?

I am trying to make a Simulator game on Roblox but I just can't seem to get the leader stats to work or it does work and my click event thing doesn't work, I am just following a tutorial for the scripting so I have no clue. This is my Remotes script where it says print("IS THIS WORKING") that was to see what was the problem. Basically that doesn't run or there's another problem that stops that from running, I think at least. I have other scripts and I will put some in that I think might be neccesary but if you need more feel free to ask me.
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteData = game:GetService("ServerStorage"):WaitForChild("RemoteData")
local cooldown = 1
print("IS THIS WORKING")
replicatedStorage.Remotes.Lift.OnServerEvent:Connect(function(player)
if not remoteData:FindFirstChild(player.Name) then return "NoFolder" end
local debounce = remoteData[player.Name].Debounce
if not debounce then
debounce.Value = true
player.leaderstats.Stealth.Value = player.leaderstats.Stealth.Value + 25 *(player.leaderstats.Rebirths.Value + 1)
wait(cooldown)
debounce.Value = false
end
Stats
local serverStorage = game:GetService("ServerStorage")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local stealth = Instance.new("NumberValue")
stealth.Name = "Stealth"
stealth.Parent = leaderstats
local rebirths = Instance.new("IntValue")
rebirths.Name = "Rebirths"
rebirths.Parent = leaderstats
local Folder = Instance.new("Folder")
Folder.Name = player.Name
Folder.Parent = serverStorage.RemoteData
local debounce = Instance.new("BoolValue")
debounce.Name = "Debounce"
debounce.Parent = Folder
end)
ModuleScript
local module = {}
local replicatedStorage = game:GetService("ReplicatedStorage")
function module.Lift()
replicatedStorage.Remotes.Lift:FireServer()
end
return module
LocalScript
local module = require(script.Parent:WaitForChild("ModuleScript"))
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
script.Parent.Activated:Connect(function()
module.Lift()
end)
For my Explorer Structure click
here
local remoteData = game:GetService("ServerStorage"):WaitForChild("RemoteData")
your problem is when it waits for a child it yields the script until RemoteData is found
later in the script, it checks if RemoteData is nil or not
if not remoteData:FindFirstChild(player.Name) then return "NoFolder" end
two solutions:
first is to add a max wait for the yield so it will eventually stop if it can't find RemoteData (only useful is RemoteData is added after the script starts running)
second is the replace wait with FindFirstChild that will check immediately without waiting for it
the solution I recommend
local remoteData = game:GetService("ServerStorage"):FindFirstChild("RemoteData")
secondary solution if you have RemoteData added after the script starts and want to double-check
local remoteData = game:GetService("ServerStorage"):WaitForChild("RemoteData",20)
be sure to tell me is this works as I spent a while figuring it out

Resources