So I have no idea what is happening, I tried everything, but the molten cocoa just doesnt reappear in the correct place.
Here is my script:
local crystalFolder = game.ServerStorage.MoltenCocoa
local RNG = Random.new()
local bases = {
{Object = workspace.MoltenBase1, Weight = 4};
{Object = workspace.MoltenBase2, Weight = 7};
}
local function random(choices)
local weightSum = 0
for i = 1, #choices do
weightSum = weightSum + choices[i].Weight
end
local rand = RNG:NextInteger(0, weightSum)
for i = 1, #choices do
if rand <= choices[i].Weight then
return choices[i].Object
end
rand = rand - choices[i].Weight
end
end
local function setRandomPosition(item)
local Base = random(bases)
item.CFrame = CFrame.new(
RNG:NextInteger((-Base.Size.X / 2), (Base.Size.X / 2)),
Base.Position.Y + (Base.Size.Y / 2) + (item.Size.Y / 2),
RNG:NextInteger((-Base.Size.Z / 2), (Base.Size.Z / 2))
) * CFrame.Angles(0, math.rad(RNG:NextInteger(0, 360)), 0)
end
local function crystalTouched(crystal, hit)
if hit.Parent:FindFirstChild("Humanoid") then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
local UserStats = plr:WaitForChild('leaderstats')
local cocoaStat = UserStats.Molten
local totalCrystals = plr:WaitForChild('TotalStats').TotalMolten
cocoaStat.Value = cocoaStat.Value + (1 * plr:WaitForChild("Gamepasses").CocoaMultiplier.Value)
totalCrystals.Value = totalCrystals.Value + (1 * plr:WaitForChild("Gamepasses").CocoaMultiplier.Value)
local leveingModule = require(game.ServerScriptService.DataStorage.Leveling.LevelingModule)
leveingModule.GiveXP(plr, 14)
local achievement = plr:WaitForChild("Achievements")
local crystals1 = achievement["Molten Cocoa Collector 1"]
local crystals2 = achievement["Molten Cocoa Collector 2"]
local tab = {
["Molten Cocoa Collector 1"] = {Stat = UserStats.Cash.Value, Reward = 1150000, ReqStat = UserStats.Molten.Value, ReqVal = 30},
["Molten Cocoa Collector 2"] = {Stat = UserStats.Cash.Value, Reward = 1000000000, ReqStat = UserStats.Molten.Value, ReqVal = 100},
}
local add = (1 * plr:WaitForChild("Gamepasses").CocoaMultiplier.Value)
cocoaStat.Value = cocoaStat.Value + add
totalCrystals.Value = totalCrystals.Value + add
crystals1.Have.Value = crystals1.Have.Value + add
crystals2.Have.Value = crystals2.Have.Value + add
local moduler = require(game.ServerScriptService.DataStorage.Achievements.Moduler)
if crystals1.Have.Value >= crystals1.Requirement.Value then
moduler[crystals1.Name](tab, achievement, crystals1.Name)
end
if crystals2.Have.Value >= crystals2.Requirement.Value then
moduler[crystals2.Name](tab, achievement, crystals2.Name)
end
crystal.Parent = crystalFolder
print("Yay")
--wait(math.random(8,20))
wait(1)
print("Waited")
crystal.Parent = workspace.MoltenCocoa
setRandomPosition(crystal)
end
end
local function debounce(func)
local deb = false
return function()
if not deb then
deb = true
print("BEFORE FUNC")
func()
deb = false
print("AFTER FUNC")
end
end
end
script.Parent.Touched:Connect(function(hit)
debounce(crystalTouched(script.Parent, hit))
end)
Here is where it spawns/where it is supposed to spawn:
