-
trying to make a spin to win wheel, it basically works except theres an issue with it i can’t figure out
-
the wheel spins and such but when the wheel stops, it only lands on prize1 position and not any other prizes that are in that folder?
-
i tried to change position value and rotation and etc i can’t figure out what to do at this point
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local RemoteEvent = ReplicatedStorage:WaitForChild("SpinWheelEvent")
local RewardsFolder = ReplicatedStorage:WaitForChild("Rewards")
-- Define the rarity of each prize (lower values are rarer)
local rarityValues = {
Prize1 = 5,
Prize2 = 10,
Prize3 = 3,
-- Add more prizes as needed
}
local isSpinning = false -- Track whether the wheel is currently spinning
local function spinWheel(player)
if isSpinning then
return -- Don't allow multiple spins while the wheel is already spinning
end
isSpinning = true -- Set to true to prevent multiple spins
-- Check if the player has enough spins to spin the wheel
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then
warn("Leaderstats folder not found.")
isSpinning = false -- Reset spinning flag
return
end
local spinsValue = leaderstats:FindFirstChild("Spins")
if not spinsValue or spinsValue.Value <= 0 then
warn("Not enough spins to spin the wheel.")
isSpinning = false -- Reset spinning flag
return
end
-- Deduct 1 spin from the player
spinsValue.Value = spinsValue.Value - 1
local frame = player.PlayerGui:FindFirstChild("Spinner").Frame
local spinwheel = frame.SpinWheel
local WheelPrizes = spinwheel:FindFirstChild("WheelPrizes") -- this is the folder that contains ImageLabels of the prizes that are on the Wheel
if not WheelPrizes then
warn("WheelPrizes folder not found.")
isSpinning = false -- Reset spinning flag
return
end
local arrow = frame.Arrow
local prizes = WheelPrizes:GetChildren()
-- Filter prizes based on names
local validPrizes = {}
for _, prize in ipairs(prizes) do
if prize:IsA("ImageLabel") then
table.insert(validPrizes, prize)
end
end
-- Calculate the arrow's rotation
local arrowRotation = math.deg(math.atan2(arrow.Position.Y.Offset, arrow.Position.X.Offset))
if arrowRotation < 0 then
arrowRotation = arrowRotation + 360
end
-- Choose a random prize to land on
local selectedPrize = validPrizes[math.random(#validPrizes)]
-- Calculate the rotation goal to land on the selected prize
local rotationDiff = selectedPrize.Position.X.Offset - spinwheel.Rotation % 360
local selectedRotationGoal = 360 * 5 + arrowRotation + rotationDiff / 2
-- Tween the Rotation property for a smooth spinning effect
local rotateTweenGoal = { Rotation = spinwheel.Rotation + selectedRotationGoal }
local rotateTweenInfo = TweenInfo.new(2, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0)
local rotateTween = TweenService:Create(spinwheel, rotateTweenInfo, rotateTweenGoal)
rotateTween:Play()
wait(3) -- Wait for 3 seconds before stopping the wheel
-- Stop the wheel immediately
rotateTween:Cancel()
-- Give the player the corresponding reward for the won prize
local reward = RewardsFolder:FindFirstChild(selectedPrize.Name)
if reward then
if leaderstats:FindFirstChild("cash") then
leaderstats.cash.Value = leaderstats.cash.Value + reward.Value
else
warn("Cash value not found in leaderstats.")
end
else
warn("Reward not found for prize:", selectedPrize.Name)
end
-- Trigger client to update UI or handle the result
RemoteEvent:FireClient(player, "WheelStopped", selectedPrize)
isSpinning = false -- Reset spinning flag after the wheel has stopped
end
RemoteEvent.OnServerEvent:Connect(spinWheel)