Button script isn't working!

Hi! I want to make it so if the player clicks the button, a track piece of a cart ride will appear. You can press it multiple times, and it will make a cart ride. Here’s my code:

local tracks = game.ReplicatedStorage.Tracks
local buttons = {}
local currentPoint = workspace.Start.Hit.Position
for i, v in ipairs(script.Parent:GetChildren()) do
	if v.ClassName == "TextButton" then 
		table.insert(buttons, v) 
	end
end

for i, button in ipairs(buttons) do
	button.Activated:Connect(function()
		local trackType = button.Text
		local trackToClone = tracks[trackType]
		local newTrack = trackToClone:Clone()
		newTrack.Parent = game.Workspace
		newTrack.PrimaryPart.Position = currentPoint
		currentPoint = newTrack.EndPos
	end)
end

However, there are many errors.

  1. The primary part of the track is teleporting to the correct location, but the rest of the track isn’t (the model itself).

Here’s what my explorer looks like:
Screenshot 2024-09-05 at 6.16.06 PM

If anyone has a solution, please respond ASAP!

1 Like

you need to do endpos.position
you specified the instance and you need to get its .Position Property
Updated:

local tracks = game.ReplicatedStorage.Tracks
local buttons = {}
local currentPoint = workspace.Start.Hit.Position
for i, v in ipairs(script.Parent:GetChildren()) do
	if v.ClassName == "TextButton" then 
		table.insert(buttons, v) 
	end
end

for i, button in pairs(buttons) do
	button.Activated:Connect(function()
		local trackType = button.Text
		local trackToClone = tracks[trackType]
		local newTrack = trackToClone:Clone()
		newTrack.Parent = game.Workspace
		newTrack.PrimaryPart.Position = currentPoint
		currentPoint = newTrack.EndPos.Position
	end)
end

Hey

for your first issue I’m assuming that it works for the first click?

currentPoint = newTrack.EndPos -- this is the issue

To fix this use;

currentPoint = newTrack.EndPos.CFrame  -- this is the fix i recommend; 

or

currentPoint = newTrack.EndPos.Position -- this is the fix that will work with ur current code; 

for your second issue you can use

Your making currentPoint an Object the endPosition of the current track model when its expecting a position (or i will later recommend using a CFrame)

newTrack:PivotTo(workspace.Start.Hit.CFrame) --pivot to takes a cframe instead of position so will also change the rotation

Note
I dont think this will make ur code fully functional there is still some logic errors here that need to fixed you’re gonna need to figure out the offset you need to move the track away from the end point ect

1 Like

Lemme just rewrite that for you

local tracks = game.ReplicatedStorage.Tracks
local buttons = {}
local currentPoint = workspace.Start.Hit.CFrame
for i, v in ipairs(script.Parent:GetChildren()) do
	if v.ClassName == "TextButton" then 
		table.insert(buttons, v) 
	end
end

for i, button in ipairs(buttons) do
	button.Activated:Connect(function()
		local trackType = button.Text
		local trackToClone = tracks[trackType]
		local newTrack = trackToClone:Clone()
		newTrack.Parent = game.Workspace
		newTrack:PivotTo(currentpoint) -- Solves error 2
		currentPoint = newTrack.EndPos.CFrame -- Solves error 1
	end)
end

To solve error 2, you want to use :PivotTo to move the whole model, not only its primary part. PivotTo accepts a cframe only, so you have to change some parts of your code to accommodate this (if any). For error 1, you are assigning the currentpoint var the endpos part, not its position, which is causing the error

1 Like

Thank you! Now I just need to add more tracks and stuff!


(and fix the ui lol)