Need Help on ProximityPrompt Hold (With Animation)

Hello, guys! I’ve been trying to make a cash register and it is almost done. But I have a problem.
So, I made a script for the cash register but when I hold to proximity prompt the animation doesn’t play.

-- SETTINGS --

-- [Cash Register] --

local Cooldown = 8 -- The couldown after the rob
local ProximityCooldown = 5 -- The cooldown of the proximity prompt
local MoneyAward = math.random(100, 2000) -- local Money = math.random(minimum, maximum) or local Money = amount

local canBeRobbed = true

local Alarm = "" -- The alarm sound Id

-- [Webhook] --

local WebHookURL = "" -- The webhook URL

-- [Other] --

local proximityPrompt = script.Parent:WaitForChild("Hitbox"):WaitForChild("ProximityPrompt")
local MoneyService = game:GetService("DataStoreService"):GetDataStore("MoneyService")

-- SCRIPTS -- 

--[[ DELETE THIS LINE IF YOU DON'T HAVE A LEADERSTATS SCRIPT

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"
	
	local money = Instance.new("NumberValue", leaderstats)
	money.Value = "Money"
	
	local data 
	
	local success, errormessage = pcall(function()
		data = {
			MoneyService:GetAsync(plr.UserId)
		}
	end)
end)


--]] --DELETE THIS LINE IF YOU DON'T HAVE A LEADERSTATS SCRIPT

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"

	local money = Instance.new("NumberValue", leaderstats)
	money.Value = "Money"

	local data 

	local success, errormessage = pcall(function()
		data = {
			MoneyService:GetAsync(plr.UserId)
		}
	end)
end)


game.Players.PlayerRemoving:Connect(function(plr)
	local data = {plr.leaderstats.Money}
	
	local success, errormessage = pcall(function()
		MoneyService:SetAsync(plr.UserId, data[1])
	end)
end)


while wait() do
	if canBeRobbed == true then
		proximityPrompt.Enabled = true
	else
		proximityPrompt.Enabled = false
	end
	proximityPrompt.HoldDuration = ProximityCooldown
end

local robbingAnim = script.RobbingAnim

proximityPrompt.PromptButtonHoldBegan:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local hum = char:FindFirstChild("Humanoid")
		hum:LoadAnimation(robbingAnim)
		robbingAnim:Play()
	end)
end)

proximityPrompt.PromptButtonHoldEnded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local hum = char:FindFirstChild("Humanoid")
		hum:LoadAnimation(robbingAnim)
		robbingAnim:Stop()
	end)
end)

proximityPrompt.Triggered:Connect(function(plr)
	plr.leaderstats.Money.Value += MoneyAward
	canBeRobbed = false
	wait(Cooldown)
	canBeRobbed = true
end)

I tried getting help from roblox studio community servers but I couldn’t find anything useful

1 Like

try this But I think the animation won’t stop playing

-- SETTINGS --

-- [Cash Register] --

local Cooldown = 8 -- The couldown after the rob
local ProximityCooldown = 5 -- The cooldown of the proximity prompt
local MoneyAward = math.random(100, 2000) -- local Money = math.random(minimum, maximum) or local Money = amount

local canBeRobbed = true

local Alarm = "" -- The alarm sound Id

-- [Webhook] --

local WebHookURL = "" -- The webhook URL

-- [Other] --

local proximityPrompt = script.Parent:WaitForChild("Hitbox"):WaitForChild("ProximityPrompt")
local MoneyService = game:GetService("DataStoreService"):GetDataStore("MoneyService")

-- SCRIPTS -- 

--[[ DELETE THIS LINE IF YOU DON'T HAVE A LEADERSTATS SCRIPT

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"
	
	local money = Instance.new("NumberValue", leaderstats)
	money.Value = "Money"
	
	local data 
	
	local success, errormessage = pcall(function()
		data = {
			MoneyService:GetAsync(plr.UserId)
		}
	end)
end)


--]] --DELETE THIS LINE IF YOU DON'T HAVE A LEADERSTATS SCRIPT

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"

	local money = Instance.new("NumberValue", leaderstats)
	money.Value = "Money"

	local data 

	local success, errormessage = pcall(function()
		data = {
			MoneyService:GetAsync(plr.UserId)
		}
	end)
end)


game.Players.PlayerRemoving:Connect(function(plr)
	local data = {plr.leaderstats.Money}

	local success, errormessage = pcall(function()
		MoneyService:SetAsync(plr.UserId, data[1])
	end)
end)


while wait() do
	if canBeRobbed == true then
		proximityPrompt.Enabled = true
	else
		proximityPrompt.Enabled = false
	end
	proximityPrompt.HoldDuration = ProximityCooldown
end

local robbingAnim = script.RobbingAnim

proximityPrompt.PromptButtonHoldBegan:Connect(function(plr)
	local char = plr.Character 
	local PlayAnim = char.Humanoid:LoadAnimation(robbingAnim)
	PlayAnim:Play()
end)

proximityPrompt.PromptButtonHoldEnded:Connect(function(plr)
	local char = plr.Character 
	local PlayAnim = char.Humanoid:LoadAnimation(robbingAnim)
	PlayAnim:Stop()
end)

proximityPrompt.Triggered:Connect(function(plr)
	plr.leaderstats.Money.Value += MoneyAward
	canBeRobbed = false
	wait(Cooldown)
	canBeRobbed = true
end)

Your code yields here, the bottom events cannot be ran because it’s looping here, put the while loop on the bottom.

First of all, I’m getting this error:
image
and second of all the animation is still not working

Ik this won’t help you, but please use Animator method to load an animation. Humanoid method is deprecated

char.Humanoid.Animator:LoadAnimation()

1 Like

yeah but how do I stop the animation

Get the loaded animation variable and do
Animation:Stop()

1 Like

It still doesn’t play the animation (forgot to mention that it is a server script)