I’m trying to achive this on my sword model but I’m having this error can someone help me with my script (I’m not good at the tweens)
- What am I trying to achive like this:
[Deleted For Privacy Reasons]
- What I got:
[Deleted For Privacy Reasons]
The script (Local Script)
local sword = game.Workspace:WaitForChild("swordTest"):WaitForChild("LinkedSword"):WaitForChild("Main")
local ProximityPrompt = game.Workspace:WaitForChild("swordTest"):WaitForChild("LinkedSword"):WaitForChild("PromptPart").ProximityPrompt
local Player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AwardBadgeEvent = ReplicatedStorage:WaitForChild("AwardBadgeEvent")
local chancePercentage = 100
local isAttempted = false
local particle1 = ProximityPrompt.Parent.Parent.Parent:WaitForChild("testscript").Middle:WaitForChild("1")
local particle2 = ProximityPrompt.Parent.Parent.Parent:WaitForChild("testscript").Middle:WaitForChild("2")
local particle3 = ProximityPrompt.Parent.Parent.Parent:WaitForChild("testscript").Middle:WaitForChild("3")
local swordPullSound = game.SoundService.Misc:WaitForChild("SwordPull")
local isHolding = false
local holdStartedTime = 0
local TweenService = game:GetService("TweenService")
local function fadeOutSound(sound, duration)
local initialVolume = sound.Volume
local step = initialVolume / (duration / 0.1)
task.spawn(function()
while sound.Volume > 0 do
sound.Volume = sound.Volume - step
task.wait(0.1)
end
sound:Stop()
sound.Volume = initialVolume
end)
end
local function onPromptHold()
isHolding = true
holdStartedTime = tick()
swordPullSound:Play()
task.spawn(function()
while isHolding do
local elapsed = tick() - holdStartedTime
if elapsed >= 3 and elapsed < 6 then
particle1.Enabled = true
particle2.Enabled = false
particle3.Enabled = false
elseif elapsed >= 6 and elapsed < 9 then
particle1.Enabled = true
particle2.Enabled = true
particle3.Enabled = false
elseif elapsed >= 9 then
particle1.Enabled = true
particle2.Enabled = true
particle3.Enabled = true
end
task.wait(0.1)
end
end)
end
local function tweenSword()
ProximityPrompt.Enabled = false
local tweenInfo = TweenInfo.new(
1,
Enum.EasingStyle.Linear,
Enum.EasingDirection.InOut,
0,
false,
0
)
local goal = {
Position = sword.Position + Vector3.new(0, 10, 0),
Orientation = sword.Orientation + Vector3.new(0, 360, 0)
}
local tween = TweenService:Create(sword, tweenInfo, goal)
local emitParticle = sword:WaitForChild("SwordParticle")
tween:Play()
sword.Spin:Play()
tween.Completed:Connect(function()
local spinInfo = TweenInfo.new(
1.5,
Enum.EasingStyle.Linear
)
local spinGoal = {
Orientation = sword.Orientation + Vector3.new(0, 1440, 0)
}
local spinTween = TweenService:Create(sword, spinInfo, spinGoal)
spinTween:Play()
spinTween.Completed:Connect(function()
sword.Transparency = 1
emitParticle:Emit(50)
sword.Disappear:Play()
sword.CanCollide = false
emitParticle.Enabled = false
end)
end)
end
local function onPromptTriggered()
isHolding = false
fadeOutSound(swordPullSound, 1)
particle1.Enabled = false
particle2.Enabled = false
particle3.Enabled = false
if not isAttempted then
isAttempted = true
local randomNumber = math.random(0, 100)
if randomNumber < chancePercentage then
tweenSword()
task.wait(3.5)
AwardBadgeEvent:FireServer(true)
game.ReplicatedStorage.Events.LocalInvokeFunction:Fire("TopMessage", "You Pulled The Sword Successfully.")
game.SoundService.Misc.PullSuccess:Play()
else
game.ReplicatedStorage.Events.LocalInvokeFunction:Fire("TopMessage", "You Failed To Pull The Sword.")
game.SoundService.Misc.PopUpSwordFail:Play()
print("Try again")
end
else
game.ReplicatedStorage.Events.LocalInvokeFunction:Fire("TopMessage", "You already tried.")
game.SoundService.Misc.PopUpSwordFail:Play()
end
end
ProximityPrompt.PromptButtonHoldBegan:Connect(onPromptHold)
ProximityPrompt.Triggered:Connect(onPromptTriggered)
ProximityPrompt.PromptButtonHoldEnded:Connect(function()
isHolding = false
fadeOutSound(swordPullSound, 1)
particle1.Enabled = false
particle2.Enabled = false
particle3.Enabled = false
end)