Advertisement
irishWarlock89

Treasure Hunt Script

Feb 13th, 2025
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.10 KB | Source Code | 0 0
  1. --need a basepart named "treasure", OR a model named "treasure" with a child part named "main"
  2. --need a folder in workspace named "HidingSpots" which holds all baseparts which represent potential hiding spots
  3. ----for the treasure
  4. --File->Game Settings->Security->Enable Studio Access to API Services->ON
  5.  
  6. if script.Parent ~= game:GetService("ServerScriptService") then
  7.     script.Parent = game:GetService("ServerScriptService")
  8. end
  9.  
  10. local DataStoreService = game:GetService("DataStoreService")
  11. local PlayerTreasuresStore = DataStoreService:GetDataStore("PlayerTreasuresStore")
  12.  
  13. function savedata(player)
  14.     local success, err = pcall(function()
  15.         PlayerTreasuresStore:SetAsync(player.UserId, player.leaderstats.Treasures.Value)
  16.     end)
  17.     if success then
  18.         print("Saved player's data successfully!")
  19.     else
  20.         print(err)
  21.     end
  22. end
  23.  
  24. game.Players.PlayerRemoving:Connect(function(player)
  25.     savedata(player)
  26. end)
  27.  
  28. game:BindToClose(function()
  29.     for i, player in pairs(game.Players:GetPlayers()) do
  30.         savedata(player)
  31.     end
  32. end)
  33.  
  34.  
  35. game.Players.PlayerAdded:Connect(function(player)
  36.     local leaderstats = Instance.new("Folder")
  37.     leaderstats.Name = "leaderstats"
  38.     leaderstats.Parent = player
  39.  
  40.     local treasures = Instance.new("IntValue")
  41.     treasures.Name = "Treasures"
  42.     treasures.Parent = leaderstats
  43.     treasures.Value = PlayerTreasuresStore:GetAsync(player.UserId) or 0
  44. end)
  45.  
  46.  
  47.  
  48. local treasure = game:GetService("ServerStorage"):WaitForChild("treasure")
  49. if treasure:IsA("Model") then
  50.     for i, P in pairs(treasure:GetChildren()) do
  51.         if P:IsA("BasePart") then
  52.             P.CanCollide = false
  53.         end
  54.     end
  55.     treasure.PrimaryPart = treasure.main
  56. elseif treasure:IsA("BasePart") then
  57.     treasure.CanCollide = false
  58. else
  59.     error("No idea what the treasure is.")
  60. end
  61.  
  62. local particle = Instance.new("ParticleEmitter")
  63. particle.Color = ColorSequence.new(Color3.new(0, 1, 0))
  64. particle.LightEmission = 0.5
  65. particle.LightInfluence = 1
  66. particle.Size = NumberSequence.new(1.5, 0)
  67. particle.Lifetime = NumberRange.new(1, 4)
  68. particle.Rate = 20
  69. particle.Speed = NumberRange.new(2, 8)
  70. particle.SpreadAngle = Vector2.new(180, 180)
  71.  
  72. local particle2 = Instance.new("ParticleEmitter")
  73. particle2.Color = ColorSequence.new(Color3.new(0.984314, 1, 0.00392157))
  74. particle2.LightEmission = 0.5
  75. particle2.LightInfluence = 1
  76. particle2.Size = NumberSequence.new(1.5, 0)
  77. particle2.Lifetime = NumberRange.new(1, 4)
  78. particle2.Rate = 20
  79. particle2.Speed = NumberRange.new(16, 16)
  80. particle2.SpreadAngle = Vector2.new(180, 180)
  81. particle2.Enabled = false
  82.  
  83. if treasure:IsA("Model") then
  84.     particle.Parent = treasure.main
  85.     particle2.Parent = treasure.main
  86. elseif treasure:IsA("BasePart") then
  87.     particle.Parent = treasure
  88.     particle2.Parent = treasure
  89. end
  90.  
  91. local hidingSpotsFolder = game.Workspace.HidingSpots
  92. local hidingSpots = {}
  93. local spot = nil
  94.  
  95. for i, part in pairs(hidingSpotsFolder:GetChildren()) do
  96.     if part:IsA("BasePart") then
  97.         part.Anchored = true
  98.         part.Transparency = 1
  99.         part.CanCollide = false
  100.         part.Orientation = Vector3.new(0, 0, 0)
  101.         table.insert(hidingSpots, part)
  102.     end
  103. end
  104.  
  105. function chooseNewSpot()
  106.     local newSpot = spot
  107.     while newSpot == spot do
  108.         newSpot = hidingSpots[math.random(1, #hidingSpots)]
  109.     end
  110.     return newSpot
  111. end
  112.  
  113. function spawnTreasure()
  114.     spot = chooseNewSpot()
  115.     treasure.Parent = game.Workspace
  116.     if treasure:IsA("Model") then
  117.         treasure:MoveTo(spot.Position + Vector3.new(0, 0, 0))
  118.     elseif treasure:IsA("BasePart") then
  119.         treasure.CFrame = spot.CFrame + CFrame.new(0, 0, 0)
  120.     end
  121. end
  122.  
  123. local debounce = false
  124. function treasureTouched(otherPart)
  125.     local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
  126.     if player then
  127.         if debounce == false then
  128.             debounce = true
  129.             particle2:Emit(200)
  130.             task.wait(0.5)
  131.             treasure.Parent = game:GetService("ServerStorage")
  132.             player.leaderstats.Treasures.Value = player.leaderstats.Treasures.Value + 1
  133.             task.wait(2)
  134.             debounce = false
  135.             spawnTreasure()
  136.         end
  137.     end
  138. end
  139.  
  140. if treasure:IsA("Model") then
  141.     treasure.main.Touched:Connect(treasureTouched)
  142. elseif treasure:IsA("BasePart") then
  143.     treasure.Touched:Connect(treasureTouched)
  144. end
  145.  
  146.  
  147. spawnTreasure()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement