Need help solving a bug

I need help to solve this problem, so i made a script on my game where if u get close to an object whit a proximity prompt on it it does a fade animation using tween service and a highlight but if the player approaches and moves away very quickly from the object it glitches out

here is an example :

this is what happens if the player does it too fast

this is the code


local ProximityPromptService = game:GetService("ProximityPromptService")
local TweenService = game:GetService("TweenService")


local player = game.Players.LocalPlayer


--effects
local particle = script.ParticleEmitter
local light = script.PointLight


---{Functions}---

ProximityPromptService.PromptShown:Connect(function(ProximityPrompt)

	if ProximityPrompt.Parent and ProximityPrompt.Parent:IsA("BasePart") then

		
		local Highlight = Instance.new("Highlight", ProximityPrompt.Parent); Highlight.FillColor = Color3.fromRGB(255, 255, 255); Highlight.FillTransparency = 1; Highlight.OutlineTransparency = 1

		particle:Clone().Parent = Highlight.Parent


		local light = light:Clone()
		light.Parent = Highlight.Parent

		local goal = {}
		goal.OutlineTransparency = 0
		
		local goal2 = {}
		goal2.Brightness = 0.6
		
		local tweenInfo = TweenInfo.new(0.5)
		local tweenInfo2 = TweenInfo.new(0.5)
		
		local tween2 = TweenService:Create(light, tweenInfo2, goal2)
		local tween = TweenService:Create(Highlight, tweenInfo, goal)
        
		tween:Play()
		tween2:Play()
		
		player.PlayerGui.InteractButton.TextLabel.Text = "(".. ProximityPrompt.KeyboardKeyCode.Name .. ")"
		player.PlayerGui.InteractButton.Enabled = true
		
	end

end)

ProximityPromptService.PromptHidden:Connect(function(ProximityPrompt)

	if ProximityPrompt.Parent and ProximityPrompt.Parent:FindFirstChildOfClass("Highlight") then

		local Highlight = ProximityPrompt.Parent:FindFirstChildOfClass("Highlight")
		player.PlayerGui.InteractButton.Enabled = false
		
		local particle = Highlight.Parent:FindFirstChild("ParticleEmitter")
		local light = Highlight.Parent:FindFirstChild("PointLight")
		particle.Enabled = false
		
		local goal = {}
		goal.OutlineTransparency = 1

		local goal2 = {}
		goal2.Brightness = 0

		local tweenInfo = TweenInfo.new(0.5)
		local tweenInfo2 = TweenInfo.new(0.5)

		local tween2 = TweenService:Create(light, tweenInfo2, goal2)
		local tween = TweenService:Create(Highlight, tweenInfo, goal)

		tween:Play()
		tween2:Play()
		
		tween.Completed:Connect(function()
			particle:Destroy()
			Highlight:Destroy()
			light:Destroy()
		end)
	end

end)

thanks!

Sadly I cant watch the videos idk why, its only a black screen for me…

About your code. Seems that when PromptHidden fires, you are creating and playing a Tween while the previous Tween (the one from PromptShow) still playing. Thats why when player approaches and moves away quickly it glitches.

You should Cancel the previous Tweens before Playing the new one when PromptHidden fires.

You could pass it to the function or create variables outside the function so you can access the previous Tweens and stop them before playing the new one

1 Like

Thanks it cost me a bit but i fixed it thanks

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.