So, to do this you would want to put a ServerScript inside of the Orb and write this code. I'll explain how it works below the code.
Setup
ServerScript inside of the part
local incremental = 1
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
script.Sound.PlayOnRemove = true
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
local starOrbs = player:WaitForChild("leaderstats"):FindFirstChild("StarOrbs")
starOrbs.Value = starOrbs.Value + incremental
wait()
script.Parent:Destroy()
end
end)
So, the first line is how much their StarOrbs value will increase by. Line 4 will make sure to play the sound when the part is destroyed. Everything else is pretty much the same except you don't need a debounce variable for this since the part will be removed when it is touched.
If you wanted to do a DataStore, there's 2 ways. ROBLOX DataStore or DataStore2. Either way works.
Depends on how you want to do it, DataStore is better for Tables, and DataStore2 is better for variables.
I would put a folder inside of the player when they join and put a bool value for every coin there is, like this. I would assume there is a folder in ReplicatedStorage to hold all the coins.
game.Players.PlayerAdded:Connect(function(player)
local coinsFolder = Instance.new("Folder",player)
coinsFolder.Name = "Coins"
for _,pCoin in pairs(game:GetService("ReplicatedStorage"):WaitForChild("CoinModels"):GetChildren()) do
local coinBool = Instance.new("BoolValue",coinsFolder)
coinBool.Name = pCoin.Name
end
end)
I then would create a dictionary for the names and values, like so.
local coinsDictionary =
{
["Coin1"] = true,
["Coin2"] = false
}
After that, you can save that and load that with DataStore, using GetAsync and SetAsync.
GetAsync is to get the saved values. SetAsync is to set the new values. You can also change the values in the Dictionary by doing this.
local coinsDictionary =
{
["Coin1"] = true,
["Coin2"] = false
}
game.Players.PlayerAdded:Connect(function(player)
coinsDictionary.Coin1 = player:WaitForChild("Coins"):FindFirstChild("Coin1").Value
coinsDictionary.Coin2 = player:WaitForChild("Coins"):FindFirstChild("Coin2").Value
end)
Then you would use :GetAsync() to get the table and set the values & :SetAsync() to set the new values when the player leaves.
DataStore Wiki
Dev Forum Table
Dev Forum Dictionary
Hope this helped! If this is what you're looking for, let me know by selecting this as an answer!
3 comments
Well, if you don't want it to happen again, don't set db back to true.
It's also not working because LocalScripts are ClientSided, not ServerSided. So, any player will be able to collect it.
Please provide an attempt at solving this.