I wrote a door save system.
That is, if the user previously bought them, then when re-entering the game, they must be open.
My code works, but the door doesn't save at all.
-- DoorsDataStore
-- Save Stats Doors
local opend = false
local datastorage = game:GetService("DataStoreService")
local isitopen_1 = datastorage:GetDataStore("Door")
game.Players.PlayerAdded:Connect(function(player)
local boolValueDoors = Instance.new("Folder")
boolValueDoors.Name = "BoolValueDoors"
boolValueDoors.Parent = player
local door_1 = Instance.new("BoolValue")
door_1.Parent = boolValueDoors
door_1.Name = "BoolValueDoor_1"
door_1.Value = isitopen_1:GetAsync(player.UserId)
print("True or False")
print(player.BoolValueDoor_1.Value)
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, erromsg = pcall(function()
isitopen_1:SetAsync(player.UserId, player.BoolValueDoor_1.Value)
end)
if erromsg then
warn("Error")
end
end)
-- TouchDoor
script.Parent.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if (humanoid ~= nil) then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player.leaderstats.Coins.Value >= script.Parent.Price.Value then
player.leaderstats.Coins.Value -= script.Parent.Price.Value
player.leaderstats.Level.Value += 1
script.Parent:Destroy()
player.BoolValueDoors.BoolValueDoor_1.Value = true
print("Save Door")
end
end
end)
I tried writing this code in different ways, in different versions, tried through validation. My code still doesn't do what I want.
There are multiple possible problems I see:
1.
SetAsync takes a string as the first argument, you are giving it a number value. To fix this use tostring(player.UserId)
2.
When the player first joins, the value will be set to nil, because there is no data in the datastore under the UserId, and nil is not a boolean.
I’m not sure if these are actual issues and I currently cannot check if these are real problems because I’m not on my computer but they may be something you want to change.
Also it would be nice to know if you encountered any errors when executing the code.
You should also make trigger a remote event that opens the door on the clientside in the PlayerAdded function if the value is true.
Related
bear with me but this is my first post so unsure of general formats etc.
I am currently testing with my first data store system on ROBLOX. After following a tutorial I managed to get a data store to store, load and save a cash value specified to the userID that went into the leaderboard. I am currently trying to edit the system to work with a value saved to the player, rather than a leaderstat value but it doesn't seem to be either loading or saving the value correctly.
If you take a look at the code below it is set up to print out strings to confirm the saving/loading has worked but it does not either load or save the value correctly.
Currently the script loads the value and a separate localscript displays the value as text to a display gui with two buttons than can either increase or decrease the value.
I am very new to ROBLOX Lua, so any help would be greatly appreciated.
Script located within ServerScriptService:
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("DataStore")
local data2
game.Players.PlayerAdded:Connect(function(player)
local SusTune = Instance.new("Folder")
SusTune.Name = "SusTune"
SusTune.Parent = player
local fcamber = Instance.new("IntValue")
fcamber.Name = "fCamber"
fcamber.Parent = SusTune
local success, errormessage = pcall(function()
data2 = myDataStore:GetAsync(player.UserId.."-FC")
end)
if success then
fcamber.Value = data2
print("Successfully loaded data!")
print(fcamber.Value)
else
warn(errormessage)
print("There was an error while getting your data.")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-FC",player.SusTune.fCamber.Value)
end)
if success then
print("Data saved successfully!")
else
print("There was an error when saving data.")
warn(errormessage)
end
end)
LocalScript located within gui button:
local counterDisplay = script.Parent
local increaseButton = script.Parent.Parent.Increase
local decreaseButton = script.Parent.Parent.Decrease
local counter = game.Players.LocalPlayer.SusTune.fCamber.Value
counterDisplay.Text = counter
increaseButton.MouseButton1Click:Connect(function()
print("+1 added")
counter += 1
game.Players.LocalPlayer.SusTune.fCamber.Value = counter
counterDisplay.Text = counter
end)
decreaseButton.MouseButton1Click:Connect(function()
print("-1 added")
counter -= 1
game.Players.LocalPlayer.SusTune.fCamber.Value = counter
counterDisplay.Text = counter
end)
I have personally tried comparing this version to the working cash version but cannot work out why its not working. If its something really silly or simple I am going to hate myself and feel like an idiot, but been staring at it for an hour so need some help. Many thanks in advance :)
EDIT:
I think I have managed to narrow down the error.
When running the game and using console the buttons do not change the fCamber.Value BUT when running it in studio you can visually see the value changing with each button click, also confirmed by the ‘+/-1 added’ which is outputted in both studio and Roblox console.
After using SetASync in the main script, I had the script output the value it had just saved using a GetASync immediately afterwards (for testing purposes) and it seems the value itself is not saving to the data store.
I’m not sure if this extra data will benefit or not, but thought it would be a good detail to add.
z32
I am starting to program in roblox, and I am making a Menu only for users who have a gamepass, I would like to know how I can make an exclusive graphical interface appear when a user has a gamepass, and the error here is that when a user buy that gamepass automatically the exclusive interface is activated to all the users of the server, I would like to know how to fix this problem.
The Script is on ServerScriptService
local MarketPlaceService = game:GetService("MarketplaceService")
local GamePass = 100725261
local Tool = game.StarterGui.ScreenGui.TPMenu
function compra(jugador)
local loTiene = false
local success, errorMensaje = pcall(function()
loTiene = MarketPlaceService:UserOwnsGamePassAsync(jugador.UserId, GamePass)
end)
if loTiene then
game.StarterGui.ScreenGui.TPMenu.Visible = true
end
end
game.Players.PlayerAdded:Connect(function(player)
compra(player)
end)
I tried to made this in a LocalScript and is not working
StarterGui is put into each Player's PlayerGui after they spawn, so editing it for one user would mean that other users would also see the changes after respawning. A better option would be to modify PlayerGui instead, like this:
function compra(jugador)
local loTiene = false
local success, errorMensaje = pcall(function()
loTiene = MarketPlaceService:UserOwnsGamePassAsync(jugador.UserId, GamePass)
end)
if loTiene then
jugador.CharacterAdded:Connect(function() -- Ran every time the player spawns
workspace:WaitForChild(jugador.Name) -- Wait for player to finish spawning
jugador.PlayerGui.ScreenGui.TPMenu.Visible = true
end)
end
end
Keep it as a Script in ServerScriptService.
local camera = workspace.CurrentCamera
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local startergui = game:GetService("StarterGui")
local char = Players.LocalPlayer.Character
local model = workspace.OceanVillagedr201["Wine Cellar"].WineDigitalkeypad
local screen = model.screen
local replicated_storage = game:GetService("ReplicatedStorage")
local CheckCode = Instance.new("RemoteEvent")
CheckCode.Name = "CheckWineCellarCodeEvent"
CheckCode.Parent = replicated_storage
local function Entercode(player)
Players.LocalPlayer.Character.Humanoid.RootPart.Anchored = true
for _,p in pairs(char:GetChildren()) do
p.Transparency = 1
end
game.StarterGui = replicated_storage.EnterWineCellarCode
end
screen.ProximityPrompt.Triggered:Connect(function(player)
Entercode()
end)
Im attempting to create a function that triggers when the proximity prompt is triggered, One piece of this Entercode() function is to toggle the playermodel's Transparency from 0 to 1 and Remove the Characters ability to move.
local function Entercode(player)
print("went")
Players.LocalPlayer.Character.Humanoid.RootPart.Anchored = true
for _,p in pairs(char:GetChildren()) do
p.Transparency = 1
end
But Im having trouble with this Piece. It keeps telling me "attempt to index nil" with anything dealing with trying to reference the Characters model. (FFC(), GetChildren(), Player.LocalPlayer.Character, etc.). I am using a local script because I plan to create a Remote Function for the result of EnterCode()
I believe your problem is that the character either was not spawned at the time of making char or is an old character (player respawned & a new character was made). A quick fix would be to redeclare char inside Entercode:
local function Entercode()
char = player.Character
char.Humanoid.RootPart.Anchored = true
for _, p in pairs(char:GetChildren()) do
if p:IsA("BasePart") then
p.Transparency = 1
end
end
replicated_storage.EnterWineCellarCode.Parent = player.PlayerGui
end
There were also a number of other errors, here are a few changes I made:
local function Entercode()
In the original code, player was a parameter but was not sent as an argument Good thing you set the player variable at the beginning of the code.
if p:IsA("BasePart") then
p.Transparency = 1
end
In the original code, you didn't check to see if p was a part or not.
replicated_storage.EnterWineCellarCode.Parent = player.PlayerGui
In the original code, you tried to set StarterGui to EnterWineCellarCode? I don't know what you were going for but I'm assuming you meant to parent EnterWineCellarCode to PlayerGui
Lastly, you might want to use GetDescendants() instead of GetChildren(). To better understand how the character works I recommend you read the wiki entry for it
can someone help me with that? I already got the code for resetting but when i open chat and say rrrrrrr, i keep dying.
here it is:
local player = game:GetService("Players").LocalPlayer
local character = player.Character
local enabled = true
local userinputservice = game:GetService("UserInputService")
userinputservice.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.R and enabled then
character.Head:Remove()
enabled = false
wait(6)
enabled = true
end
end)
UserInputService#inputBegan gives you gameProcessedEvent as the second parameter of the function.
It indicates whether the game engine internally observed this input and acted on it. Generally, this refers to UI processing, so if a button was touched or clicked from this input, gameProcessedEvent would be true. This applies to TextBoxes, such as the chat.
All you need to check if if gameProcessedEvent is true.
local UserInputService = game:GetService("UserInputService");
local PlayersService = game:GetService("Players");
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return false end
if (input.KeyCode == Enum.KeyCode.R) then
PlayersService.LocalPlayer.Character.Head:Destroy()
end
end)
I am trying to make an in-game system for banning players. I have a button that fires a remote event with a player's name and a message for why they were banned. But every time I hit the button, I get this error :
ServerScriptService.Event_Handler:21: attempt to call a nil value
I have no idea why this is not working can someone help me understand what's going wrong?
EVENT_HANDLER
local dss = game:GetService("DataStoreService")
local bands = dss:GetDataStore("banDataStore")
BanPlayer.OnServerEvent:Connect(function(player, playertoban, reason)
local pui = player.UserId
local success, errormessage = pcall(function()
bands:SetAsync("Banned-", pui, true)
end)
if success then
print("Player Successfuly Banned")
end
game.Players:FindFirstChild(playertoban):Kick(reason)
end)
Since you are searching for players by name, it's possible that you could be spelling the name wrong. In that case, game.Players:FindFirstChild() will return nil. You can sanitize this call by making sure that the player exists before calling Kick()
Also, as a side note, it looks like you are banning the player that calls the BanPlayer RemoteEvent, not the one whose name is stored in playertoban.
local dss = game:GetService("DataStoreService")
local bands = dss:GetDataStore("banDataStore")
BanPlayer.OnServerEvent:Connect(function(player, playertoban, reason)
-- check that playertoban is a real player's name
local bannedPlayer = game.Players:FindFirstChild(playertoban)
if not bannedPlayer then
warn("Could not find a player named " .. playertoban)
return
end
-- record their user-id so we can ban them when they rejoin
local pui = bannedPlayer.UserId
local success, errormessage = pcall(function()
bands:SetAsync("Banned-", pui, true)
end)
if success then
print(playertoban .. " Successfully Banned")
else
warn(string.format("Failed to ban %s permanently with error : %s", playertoban, errormessage))
end
-- remove them from the game
bannedPlayer:Kick(reason)
end)