Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Script can't find the cart when its there? Goes right to the 'else' function

Everytime I click the button it goes to the 'No Cart Found', I'm not sure why it does that. When I test it the output (Print 'v.Name') it comes up 'Player1' and the model in game.Workspace.userCarts is named 'Player1', not sure what I've done wrong.

script.Parent.removeCart.MouseButton1Down:connect(function()
	for i,v in pairs(game.Workspace.userCarts:GetChildren()) do
		if v:FindFirstChild(script.Parent.Settings.User.Value) then
			game.Workspace.userCarts[script.Parent.Settings.User.Value]:destroy()
			script.Parent.removeCart.Text = "Cart Removed"
			wait(2)
			script.Parent.removeCart.Text = "Remove Cart"
		else
			script.Parent.removeCart.Text = "No Cart Found"
			print(v.Name)
			wait(2)
			script.Parent.removeCart.Text = "Remove Cart"
		end
	end
end)


1 comment

0

Oh! I looked at it a second time and realize you can just run 'if game.Workspace.userCarts:FindFirstChild(script.Parent.Settings.User.Value) then' instead of a loop, way more efficient! XD Sorry, disregard my answer.

1 answer

✓ Accepted answer

Problem

v is the cart. When you say v:FindFirstChild(script.Parent.Settings.User.Value) you are looking for the script.Parent.Settings.User.Value inside the cart.

Solution

What you want to do is match the name of the cart to the player's. You use v.Name == script.Parent.Settings.User.Value instead.

script.Parent.removeCart.MouseButton1Down:connect(function()
    for i,v in next, game.Workspace.userCarts:GetChildren() do
        if v.Name == script.Parent.Settings.User.Value then
            game.Workspace.userCarts[script.Parent.Settings.User.Value]:destroy()
            script.Parent.removeCart.Text = "Cart Removed"
            wait(2)
            script.Parent.removeCart.Text = "Remove Cart"
        else
            script.Parent.removeCart.Text = "No Cart Found"
            print(v.Name)
            wait(2)
            script.Parent.removeCart.Text = "Remove Cart"
        end
    end
end)

1 comment

0

Thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.