Problem with TweenService (FIXED)

Okay so I’m trying to make it so there’s a PointPart and a bottle that’s connected to it, after the bottle is done spinning the pointpart is pointing at a part like touching a specific part, what I want to do is wherever the PointPart is touching, it prints that part’s name in the output. I already tried asking ChatGPT because my version was working but I don’t know how to.

l

ocal ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")

local Events = ReplicatedStorage:WaitForChild("Events")
local BottleSpin = Events:WaitForChild("BottleSpin")

local Bottle = script.Parent:WaitForChild("Bottle")
local PointPart = Bottle:WaitForChild("Point")

local TeamAssignmentFolder = workspace:WaitForChild("Stage"):WaitForChild("TeamAssignmentFolder")
local TeamParts = TeamAssignmentFolder:GetChildren()

local function getClosestPart()
	local closestPart = nil
	local shortestDistance = math.huge

	for _, part in pairs(TeamParts) do
		if part:IsA("BasePart") then
			local distance = (PointPart.Position - part.Position).magnitude
			if distance < shortestDistance then
				closestPart = part
				shortestDistance = distance
			end
		end
	end

	return closestPart
end

BottleSpin.OnServerEvent:Connect(function()
	local Info = TweenInfo.new(2, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out)
	local TweenProperty = {Orientation = Vector3.new(0, math.random(0, 360), 0)}
	local Tween = TweenService:Create(Bottle, Info, TweenProperty)

	Tween:Play()

	Tween.Completed:Wait()

	local closestPart = getClosestPart()

	if closestPart then
		print("Bottle landed on: " .. closestPart.Name)
	else
		print("Bottle did not land on any known part.")
	end
end)

image

1 Like

Please change this topic’s category to scripting support.

Sorry I couldn’t find it so I put it on this.

This will always be equal to true since shortestDistance is set to infinity here:

Try setting shortestDistance to a value such as 1, it might not work on the first try so you’ll need to test and adjust the value until you get the desired result, and I recommend making small adjustments, such as trying 0.5 or 1.5 rather than large values, since in the image the distance between the pointer part and the target doesn’t seem to be that large

Oh okay so like make the variable shortestDistance = to 0.5 or go higher

Smaller than 0.5 might also work (although if it’s too small you might start to experience issues with the part not being detected), but do be sure that it’s greater than 0

Alright I’ll try that out till like 100 at least and then update you, thank you

1 Like

Till now it keeps saying the part name is undetected

There’s an alternative way you can check to see if the part’s touching something which can give you more accurate and consistent results, by using GetPartsInPart, I’ll try modifying your script to make it work using it :slight_smile::+1:

Alright thank you, send me the script when your done :smiley:

1 Like

Here’s the new script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local Workspace = game:GetService("Workspace")

local Events = ReplicatedStorage:WaitForChild("Events")
local BottleSpin = Events:WaitForChild("BottleSpin")

local Bottle = script.Parent:WaitForChild("Bottle")
local PointPart = Bottle:WaitForChild("Point")

local Teams = { -- If you change the name of a Team or add a new one, you'll need to update this table accordingly
	["Blue Sapphires"] = true,
	["Green Emeralds"] = true,
	["Red Crimsons"] = true,
	["Yellow Goldens"] = true}

local Info = TweenInfo.new(2, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out) -- This way we're recycling the same TweenInfo

local function getTeamName()
	local ClosestParts = Workspace:GetPartsInPart(PointPart)

	for _, Part in ClosestParts do
		if Teams[Part.Name] then return Part.Name end
	end
end

local function onServerEvent()
	local TweenProperty = {Orientation = Vector3.new(0, math.random(0, 360), 0)}

	local Tween = TweenService:Create(Bottle, Info, TweenProperty)
	Tween:Play()
	Tween.Completed:Wait()

	local TeamName = getTeamName()

	if TeamName then
		print("Bottle landed on: " .. TeamName)
	else
		print("Bottle did not land on any known team or part.")
	end
end

BottleSpin.OnServerEvent:Connect(onServerEvent)

that caused an infinite yield and doesnt let the bottle spin

Which line is causing the infinite yield?


@theonlyplasa I did make a mistake in the code (forgot that GetPartsInPart returns an array… :sweat_smile:), so I’ve edited it to fix that, but I’m unsure as to what is causing the infinite yield to occur

Infinite yield possible on ‘ServerScriptService:WaitForChild(“Bottle”)’ - Studio

Line 8

That’s odd because the line is the same in the script you provided:

Did you replace the code of the original script, or are you testing my code in a new script? The test script’s parent might be different than the parent of the original script


@theonlyplasa I changed the way the script works, instead of using OverlapParams, it will now iterate over the array returned by GetPartsInPart and see if the part’s name is a valid team name, and if so, returns the name

Ohh, thats my bad, I forgot i changed the location of the script

It says that it didnt detect on what part it landed on

if you want to do some when the bottle get touched just make sure to enable “CanTouch” property and then use .Touched event to trigger when the bottle get touched.

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