Made a simple spin the wheel script, but its off. How would i make the script more right?
UI:
Script:
local replicated = game:GetService("ReplicatedStorage")
local Modules = replicated.Modules
local TweenService = game:GetService("TweenService")
local SpinFrame = script.Parent
local WheelModule = require(Modules.WheelModule)
local tweenInfo = TweenInfo.new(6, Enum.EasingStyle.Quart)
local Wheel = SpinFrame.Wheel
local MainHUD = SpinFrame.Parent
local HUD = MainHUD.HUD
function FillOutRewards(Awards)
-- fills out the rewards on the wheel
for i, v in pairs(Awards) do
local frame = script.Parent.Wheel.Awards:FindFirstChild(i)
if not frame then
warn("Could not find the frame needed.")
return false
end
local Template = script.Parent.Awards.Template:Clone()
Template.Text = "+ "..v.Name
Template.Parent = frame
Template.Visible = true
end
return true
end
function RefreshRewards()
-- sends a function that gets the available rewards, and applies them to the wheel
for i, v in pairs(Wheel.Awards:GetDescendants()) do
if v:IsA("TextLabel") then
v:Destroy()
end
end
local Rewards = game.ReplicatedStorage.Functions.CanSpin:InvokeServer("GetRewards")
FillOutRewards(Rewards)
end
local REWARD_DEGREE = math.round(51.4285714286) -- this is basicall
print(REWARD_DEGREE)
local IsSpinning = false
function ChooseReward()
--[[
Gets the randomaward from the server
Spins the wheel so that it lands correctly on the right award.
--]]
Wheel.Rotation = 0 -- reset this or else over time the angle won't be accurate.
IsSpinning = true
local CanSpin, randomReward = replicated.Functions.CanSpin:InvokeServer("CanSpin")
if not CanSpin then
script.error:Play()
return false
end
local randomRewardIndex = randomReward.Index
print(randomReward.Name)
local fullSpins = 6 -- how many times during the tween we want the wheel to spin before landing on our prize
local endRotation = -((360 * -fullSpins) + (REWARD_DEGREE) * (randomRewardIndex - 1))
print("rotating")
local tween = TweenService:Create(Wheel, tweenInfo, {
Rotation = endRotation
})
tween:Play() -- play the rotation animation!
tween.Completed:Connect(function()
print("Player has gotten a "..randomReward.Name)
task.delay(2, function()
IsSpinning = false
end)
return true
end)
end
local OpenValue = false
local function Delete()
for i, v in pairs(Wheel.Awards:GetDescendants()) do
if v:IsA("TextLabel") then
v:Destroy()
end
end
end
local function Close()
SpinFrame:TweenSize(UDim2.new(0,0,0,0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quart, .3)
OpenValue = false
task.wait(.3)
Delete()
end
local function Open()
OpenValue = true
RefreshRewards()
SpinFrame:TweenSize(UDim2.new(1,0,1,0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quart, .3)
end
HUD.SideRanks.Button.Spin.MouseButton1Click:Connect(function()
if not OpenValue then
Open()
else
Close()
end
end)
script.Parent.Spin.MouseButton1Click:Connect(function()
if IsSpinning then return end
ChooseReward()
end)