So, what I'm trying to make is a simple sword fighting game. I am following the tutorials of a YouTuber called AlvinBlox. I identically copied down his datastore code, while trying to learn what the code does, and the code is supposed to save the currency when the player leaves the game. For example, if you were to leave the game with 50 money, the server is supposed to save that money, and once you enter the game again, the saved data or money loads and you get ur original amount of money back. Well, in my situation that's not happening, whenever I join a game and go to the console to give myself a set amount of money, then I rejoin the game only to find out the data isn't saved at all. This is only a part of my actual stats script if you wish to see more let me know. To let you know, i also added this, "local dataStores = game:GetService("DataStoreService"):GetDataStore("MunnyDataStore")", at the very top of my code. Please help me, thank you!
local player_data
pcall(function()
player_data = dataStores:GetAsync(player.UserId.."-Munny")
end)
if player_data ~= nil then
-- Player has saved data, load it in
munny.Value = player_data
else
-- Else new player
munny.Value = defaultCash
end
local bindableEvent = Instance.new("BindableEvent")
game.Players.PlayerRemoving:Connect(function(player)
pcall(function()
dataStores:SetAsync(player.UserId.."-Munny",player.leaderstats.Munny.Value)
print("Saved")
playersLeft -= 1
bindableEvent:Fire()
end)
end)
game:BindToClose(function()
while playersLeft > 0 do
bindableEvent.Event:Wait()
end
end)
Related
So I have a roblox puzzle game that I have been working on for a while now on based on the arcade classic Q bert where the goal is to change all the colours of the bricks while avoiding enemies and getting a high score but I will be adding some features of my own so it does not get as repetitive such as additional tasks like collecting keys on platforms to unlock the door to the next level and secrets like a diamond that rarely appears once every 10 rounds and collecting one gives the player an extra dude and 10 million points.
This is how the game looks so far https://streamable.com/na46cu
The issue I am having as you can see is that the colours do in fact change but when I jump on it again it changes back to the first colour it changes to which in this case is green but I want it to stay on the first colour and make it so that it does not change until the player jumps on the brick again and later on in the game I want it to become more complex and puzzle like as the game goes on like in this example [https://www.youtube.com/watch?v=9eXJWiNXpOo][2] .
I have tried a few things like adding timers ,debounces and even separate scripts alltogether none of which have worked out for me so far and I of course went out looking for a question from somebody else that had a similar problem but so far I have been struggling to find anybody else that has the same problem.
local module = {} --module for the modulescript and for loop is created
local CollectionService = game:GetService("CollectionService")
for _, part,brick in pairs (CollectionService:GetTagged("blocks"))
do
part.Touched:Connect(function(hit) --Part connects with the touched property to the function with the parameter hit
if (hit.Parent:FindFirstChild("Humanoid"))
then
part.BrickColor = BrickColor.new ("Bright green")
wait (2)
part.BrickColor = BrickColor.new ("Eggplant")
-- local sound = workspace.Sound -- use "local sound = workspace.Sound", if there is already a sound object in the workspace
--sound.SoundId = "rbxassetid://4797903038" --replace quoted text with whatever sound id you need to use
--sound:Play()
end
end)
end
-- end)
--end
return module
I am not the best programmer but I do know the basics of programming and I have tried out various programming languages like Python and c++ all of which are not all that hard to understand once you know the basics of it all but finding out the solution to the problem is the really tricky part and so is bug fixing and troubleshooting.
I do know I could try a simple debounce system but that still does not solve the problem and it only makes it so the code only runs once and slows it down.
I have been asking all over the place for a solution to this problem but I never got an answer to it so I am trying on good old Stackoverflow for once to see if this will be the place where I get the help I need.
this should work, try it
local module = {} --module for the modulescript and for loop is created
local CollectionService = game:GetService("CollectionService")
local DidParts = {} -- Initializing another table to check if the part is already in it
for _, part,brick in pairs(CollectionService:GetTagged("blocks")) do
part.Touched:Connect(function(hit) --Part connects with the touched property to the function with the parameter hit
if hit.Parent:FindFirstChild("Humanoid") then
if table.find(DidParts,part) then
return -- checking if the part isnt in the table if it is then return
end
part.BrickColor = BrickColor.new("Bright green")
wait(2)
part.BrickColor = BrickColor.new("Eggplant")
table.insert(DidParts,part) -- when all the code has finished insert it in the table
-- local sound = workspace.Sound -- use "local sound = workspace.Sound", if there is already a sound object in the workspace
--sound.SoundId = "rbxassetid://4797903038" --replace quoted text with whatever sound id you need to use
--sound:Play()
end
end)
end
-- end)
--end
return module
So I'm trying to develop a small coin collecting game on Roblox, and am pretty new to scripting. Basically Every 0.25 - 1.5 seconds, a small part is cloned from (-254, 2, -255) (one corner of the baseplate), to (254, 2, 255) (the opposite corner). That works, but im trying to loop over every object in workspace named coin, and when one is touched, run code (for now im just trying to destroy the object but ill probably just update the Coins leaderstat). It doesn't give me any errors, it just doesnt work. I've also looked all over the internet, and cant find anything.
Code in ServerScriptStorage (spawns cubes and already works, but showed it for help.):
local runservice = game:GetService("RunService")
local interval = math.random(0.25, 1.5)
local coin = game.ServerStorage.coin
local counter = 0
local x = math.random(-254, 254)
local z = math.random(-255, 255)
runservice.Heartbeat:Connect(function(step)
counter = counter + step
if counter >= interval then
counter = counter - interval
local copy = coin:Clone()
copy.Parent = workspace
copy.Position = Vector3.new(x, 2, z)
x = math.random(-254, 254)
z = math.random(-255, 255)
interval = math.random(0.25, 1.5)
end
end)
script in desktop that handles the touching:
for _, v in pairs(workspace:GetChildren()) do
if v.Name == "coin" then
print("foo")
end
end
I hope this is enough to help!
Well as you are new to scripting in roblox let me give you your answer with good practices that may help you a lot.
First in this scenario you dont need to use Heartbeat, instead you could simple use a while loop or a recursive function and a simple wait().
Also you better create a "Coins" Folder in workspace in order to not check other objects
local waitTime = math.random(25,150)/100 --random time between 0.25 and 1.5
while true do --forever loop
wait(waitTime) --waits desired time
local coin = game.ServerStorage.coin:Clone() --cloning your coin
coin.Parent = workspace.Coins --Coins folder
coin.Position = Vector3.new(math.random(0,10),2,math.random(0,10)) --you must use your own position
coin.Touched:Connect(function(hitPart) --here is the touched function
local plr = game.Players:FindFirstChild(hitPart.Parent.Name) --check if the hitPart is part of a player
if plr then
plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 1--here you can increment your coins value in your own value
coin:Destroy()--destroys the coin
end
end)
waitTime = math.random(25,150)/100 --set a new random value to wait next
end
Also you mentioned something about loop every coin in workspace, thats why I said it is better to create a separate folder. So I made a localscript inside StarterPlayerScripts with the following code:
local RunService = game:GetService("RunService") --service
RunService.RenderStepped:Connect(function() --function on every game frame
for i,v in pairs(workspace.Coins:GetChildren()) do --loop on every coin
v.Orientation = Vector3.new(v.Orientation.X,v.Orientation.Y+5,v.Orientation.Z) --increasing Orientation just on Y in order to rotate them
end
end)
I'm doing this on localscript because is just a visual effect and it is never a good idea to send that many functions that quickly serverside. Here is the game I made for you:
https://www.roblox.com/games/5842250223/Help-for-TextBasedYoutube
You can edit the place.
In other words to answer "How to run code when any object with the same name is touched?"
You need to set the function for the object when creating it.
Edit: Also is not a good idea to send to many request to the server in short periods of time, I would recommend you to create a coin ever 2 to 3 seconds or more.
GOAL: Make an Inventory system for Hats that saves So when you Buy a hat it adds it to your inventory.
What I have Currently: Right Now I have a IntValue that is added to the player(not character) when they join. This IntValue is named "CurrentHat" and is set to the value of the players last saved hat they were wearing. After this it waits until Character loads to add that hat using CurrentHat's value to the players head by getting it from ServerStorage. Then if CurrentHat's value ever changes it connects it to add player hat function and connects it to the datastore. Below is the portion of the code where the data is added to the game and where I think the inventory data should be added to the game. All the commented out stuff is what I already tried(which failed).
function playeradded(player)
print("hello")
player:LoadCharacter()
local leaderstats = Instance.new("IntValue")
leaderstats.Name = "leaderstats"
local hat = Instance.new("StringValue")
hat.Name = ("CurrentHat")
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
leaderstats.Parent = player
hat.Parent = player
hat.Value = ds2:GetAsync(player.UserId) or math.floor(math.random(0,1))
--table.insert(hattable, hat.Value)
-- ds3:SetAsync(player.UserId,hat.Value)
--for index,value in pairs (hattable) do
-- ds3:UpdateAsync(index, function() return value end)
--end
--print(hattable[1])
--print(ds3)
local playerHat = hat.value
hat.Changed:connect(function()
ds2:SetAsync(player.UserId,hat.Value)
--ds3:SetAsync(player.UserId,table.insert(hat.Value))
--print(ds3)
end)
coins.Value = ds1:GetAsync(player.UserId) or 0
ds1:SetAsync(player.UserId,coins.Value)
A good example of what I would like to do is the popular Roblox game SwordBurst's Inventory system except with only clothing.
I would like to be able to call on the players datastore and if the hat is included in the datastore then show it in their inventory to allow them to put it on. If anyone could help me that would be awesome!
You can't save tables inside of :SetAsync() but you can inside of :UpdateAsync() so if you just do the following it should work:
local clothing = {"Hat1","Shirt1","etc"}
local datastore = game:GetService("DataStoreService"):GetDataStore("Clothing")
datastore:UpdateAsync("A_key",function()
return clothing
end)
These variables are just examples. Please change some things accordingly
So, I am making a Roblox game and it's a battle game. I want to make a cash for kill script, meaning every kill, the KILLER gets +10 cash, and you start off with 0 cash. I've already got a script, see below. I tried everything on the Internet, but NOTHING works. Even the ones from the toolbox. But instead of giving the killer the cash, it gives the killed person cash! I don't want it to be a death for kill script! Here is the code I have another question like this, it will not help. It wasn't useful(It has the leaderboard, and the killed person gets +10 cash.):
game.Players.PlayerAdded:connect(function(player)
local folder = Instance.new("Folder",player)
folder.Name = "leaderstats"
local currency1 = Instance.new("IntValue",folder)
currency1.Name = "Cash"
player.CharacterAdded:connect(function(character)
character:WaitForChild("Humanoid").Died:connect(function()
local tag = character.Humanoid:FindFirstChild("creator")
if tag ~= nil then
if tag.Value ~= nil then
currency1.Value = currency1.Value + 10 --This is the reward after the
player died.
end
end
end)
end)
end)
Thanks in advance and I hope you guys can help!
There's no way to determine that player A killed player B without keeping track of that information manually.
For example, if player A shoots a gun, the bullet that it spawns could have an added ObjectValue with a Value of player A's humanoid.
When the bullet collides with player B, you would have an easy reference back to player A and can credit them for the KO.
So, I am making a Roblox game and it's a battle game. I want to make a cash for kill script, meaning every kill, the KILLER gets +10 cash, and you start off with 0 cash. The name on the leaderboard should be Cash. Can someone please make a script for this? I've tried everything on the web, so I hope you guys can help. Please include the leaderboard Cash in the script. Thanks in advance!
Update
I've included the code for the script, but instead of giving me the cash, it gives the killed person cash. How can I fix this?
Here is the code:
game.Players.PlayerAdded:connect(function(player)
local folder = Instance.new("Folder",player)
folder.Name = "leaderstats"
local currency1 = Instance.new("IntValue",folder)
currency1.Name = "Cash"
player.CharacterAdded:connect(function(character)
character:WaitForChild("Humanoid").Died:connect(function()
local tag = character.Humanoid:FindFirstChild("creator")
if tag ~= nil then
if tag.Value ~= nil then
currency1.Value = currency1.Value + 10 --This is the reward after the player died.
end
end
end)
end)
end)
You increase the amount of money in currency1. But currency1 belongs to player, who is the who has .Died!
Assuming creator is an ObjectValue whose Value is the Player instance of the killer, we can get that player's "Cash" to increase:
....
if tag ~= nil then
local killer = tag.Value
if killer ~= nil then
-- Find the killer's leaderstats folder
local killerStats = killer:FindFirstChild("leaderstats")
if killerStats ~= nil then
-- Find the killer's Cash IntValue
local killerCash = killerStats:FindFirstChild("Cash")
-- Increase cash as before
killerCash.Value = killerCash.Value + 10
end
end
end
......
This is a bit of a complex task if you have no experience with scripting. This requires your weapon system to keep track of who kills, and feed that into the leaderboard.
If you use default Roblox weapons, this functionality is likely already in your weapons. If you are making custom weapons, you need to implement this yourself.
I suggest you check out the Roblox Wiki for an example of Leaderboards. This is a relevant article about Leaderboards.
There are also going to be plenty of leaderboards in the Toolbox that you can edit to your needs. The default Leaderboard found in the "Roblox Sets" section of the Leaderboard increases a counter named "Kills" upon kills (with default Roblox weapons), and you can edit this to increase Cash instead. However - again - depends on how you keep track of your kills.
Next time please provide code and/or more detailed description of what you have already tried, so it is easier for us to help you understand or give you pointers. Right now it looks like you haven't really searched on your own and just posted your question here right away.