Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --need a basepart named "treasure", OR a model named "treasure" with a child part named "main"
- --need a folder in workspace named "HidingSpots" which holds all baseparts which represent potential hiding spots
- ----for the treasure
- --File->Game Settings->Security->Enable Studio Access to API Services->ON
- if script.Parent ~= game:GetService("ServerScriptService") then
- script.Parent = game:GetService("ServerScriptService")
- end
- local DataStoreService = game:GetService("DataStoreService")
- local PlayerTreasuresStore = DataStoreService:GetDataStore("PlayerTreasuresStore")
- function savedata(player)
- local success, err = pcall(function()
- PlayerTreasuresStore:SetAsync(player.UserId, player.leaderstats.Treasures.Value)
- end)
- if success then
- print("Saved player's data successfully!")
- else
- print(err)
- end
- end
- game.Players.PlayerRemoving:Connect(function(player)
- savedata(player)
- end)
- game:BindToClose(function()
- for i, player in pairs(game.Players:GetPlayers()) do
- savedata(player)
- end
- end)
- game.Players.PlayerAdded:Connect(function(player)
- local leaderstats = Instance.new("Folder")
- leaderstats.Name = "leaderstats"
- leaderstats.Parent = player
- local treasures = Instance.new("IntValue")
- treasures.Name = "Treasures"
- treasures.Parent = leaderstats
- treasures.Value = PlayerTreasuresStore:GetAsync(player.UserId) or 0
- end)
- local treasure = game:GetService("ServerStorage"):WaitForChild("treasure")
- if treasure:IsA("Model") then
- for i, P in pairs(treasure:GetChildren()) do
- if P:IsA("BasePart") then
- P.CanCollide = false
- end
- end
- treasure.PrimaryPart = treasure.main
- elseif treasure:IsA("BasePart") then
- treasure.CanCollide = false
- else
- error("No idea what the treasure is.")
- end
- local particle = Instance.new("ParticleEmitter")
- particle.Color = ColorSequence.new(Color3.new(0, 1, 0))
- particle.LightEmission = 0.5
- particle.LightInfluence = 1
- particle.Size = NumberSequence.new(1.5, 0)
- particle.Lifetime = NumberRange.new(1, 4)
- particle.Rate = 20
- particle.Speed = NumberRange.new(2, 8)
- particle.SpreadAngle = Vector2.new(180, 180)
- local particle2 = Instance.new("ParticleEmitter")
- particle2.Color = ColorSequence.new(Color3.new(0.984314, 1, 0.00392157))
- particle2.LightEmission = 0.5
- particle2.LightInfluence = 1
- particle2.Size = NumberSequence.new(1.5, 0)
- particle2.Lifetime = NumberRange.new(1, 4)
- particle2.Rate = 20
- particle2.Speed = NumberRange.new(16, 16)
- particle2.SpreadAngle = Vector2.new(180, 180)
- particle2.Enabled = false
- if treasure:IsA("Model") then
- particle.Parent = treasure.main
- particle2.Parent = treasure.main
- elseif treasure:IsA("BasePart") then
- particle.Parent = treasure
- particle2.Parent = treasure
- end
- local hidingSpotsFolder = game.Workspace.HidingSpots
- local hidingSpots = {}
- local spot = nil
- for i, part in pairs(hidingSpotsFolder:GetChildren()) do
- if part:IsA("BasePart") then
- part.Anchored = true
- part.Transparency = 1
- part.CanCollide = false
- part.Orientation = Vector3.new(0, 0, 0)
- table.insert(hidingSpots, part)
- end
- end
- function chooseNewSpot()
- local newSpot = spot
- while newSpot == spot do
- newSpot = hidingSpots[math.random(1, #hidingSpots)]
- end
- return newSpot
- end
- function spawnTreasure()
- spot = chooseNewSpot()
- treasure.Parent = game.Workspace
- if treasure:IsA("Model") then
- treasure:MoveTo(spot.Position + Vector3.new(0, 0, 0))
- elseif treasure:IsA("BasePart") then
- treasure.CFrame = spot.CFrame + CFrame.new(0, 0, 0)
- end
- end
- local debounce = false
- function treasureTouched(otherPart)
- local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
- if player then
- if debounce == false then
- debounce = true
- particle2:Emit(200)
- task.wait(0.5)
- treasure.Parent = game:GetService("ServerStorage")
- player.leaderstats.Treasures.Value = player.leaderstats.Treasures.Value + 1
- task.wait(2)
- debounce = false
- spawnTreasure()
- end
- end
- end
- if treasure:IsA("Model") then
- treasure.main.Touched:Connect(treasureTouched)
- elseif treasure:IsA("BasePart") then
- treasure.Touched:Connect(treasureTouched)
- end
- spawnTreasure()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement