Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Car shop GUI Buy button not working?[SOLVED]

Asked by 9 years ago

Basically, I am making a car shop, shown here http://prntscr.com/6bx9ln and when you click one of those buttons a buy button appears at the right panel. That works, but my problem is with the buy button. When I click the buy button the car spawns and then crumbles, the script is here.


player = script.Parent.Parent.Parent.Parent.Parent cost = 5000 script.Parent.MouseButton1Click:connect(function() leaderstats = player:FindFirstChild("leaderstats") if leaderstats then money = leaderstats:FindFirstChild("Money") if money then if money.Value >= cost then spawncar = game.Lighting["CiviCar"]:clone() spawncar.Parent = workspace spawncar.MakeJoints() player.leaderstats.Money.Value = player.leaderstats.Money.Value - 5000 script.Parent.Text = "Bought" else script.Parent.FontSize = 14 script.Parent.Text = "Not enough $" return end end end end)

So yes, this spawns the car, and takes the money away but the car crumbles when spawned. This is the heirachy http://prntscr.com/6jjrbw and and this is the heirachy of the brick that when touched brings up the whole GUI http://prntscr.com/6jjrkx. Thanks.

0
Aqua's answer is incorrect. Please look at mine. EzraNehemiah_TF2 3552 — 9y

2 answers

Log in to vote
1
Answered by 9 years ago

Like what aquathorn321 said. You need to make joints. However how he did it is incorrect. MakeJoints is a function, therefore you do not use MakeJoints like this: part.MakeJoints() but like this: part:MakeJoints. You also did this in your script.

player = script.Parent.Parent.Parent.Parent.Parent
cost = 5000

script.Parent.MouseButton1Click:connect(function()
     leaderstats = player:FindFirstChild("leaderstats")
    if leaderstats then
         money = leaderstats:FindFirstChild("Money")
        if money then
            if money.Value >= cost then
                spawncar = game.Lighting:FindFirstChild("CiviCar"):clone() --Use FindFirstChild or WaitForChild instead.
        local weld = Instance.new("Weld", spawncar.Seat) --Change this to any part in the car with a unique name. If your car already has welds, remove this line!
                spawncar.Parent = workspace
                spawncar:MakeJoints() --You need welds to actually use :MakeJoints(). MakeJoints is a function! Use ":" instead of "." like as if it's a property!
                player.leaderstats.Money.Value = player.leaderstats.Money.Value - 5000
                script.Parent.Text = "Bought"
                else
                script.Parent.FontSize = 14
                script.Parent.Text = "Not enough $"
                return
            end
        end
    end
end)

Hope it helps!

0
Thanks! DeciusGalerius 20 — 9y
0
@ DeciusGalerius , You're welcome! EzraNehemiah_TF2 3552 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

If it crumbles, then the problem is with the joints, therefore you should look over the piece of the code where you establish the joints. Line 12 reads spawn.MakeJoints() when it should read spawncar:MakeJoints().

Finished Script

local player = game.Players.LocalPlayer --Assuming the script is a localscript, you can shorten this.
local cost = 5000

script.Parent.MouseButton1Click:connect(function()
local leaderstats = player:FindFirstChild("leaderstats") --You don't need nearly as many if statements as you had, since if the code results in error, it will stop running and can be recalled when the button is pressed again.
local money = leaderstats:FindFirstChild("Money")
if money.Value >= cost then
local spawncar = game.Lighting["CiviCar"]:clone()
spawncar.Parent = workspace
spawncar:MakeJoints() --Assuming you already have proper welds established in the car.
money.Value = money.Value - 5000
script.Parent.Text = "Bought"
else
script.Parent.FontSize = 14
script.Parent.Text = "Not enough $"
end
end)
0
I realized that before I made this question, and its having no affect on if it crumbles or not. Its still crumbling, maybe it has something to do with "return" DeciusGalerius 20 — 9y
0
Maybe parent it after making joints. aquathorn321 858 — 9y
0
Or you can anchor it, parent it to workspace, makejoints, and unanchor it again. I edited my answer, Try that. aquathorn321 858 — 9y
0
No, aqua, your script is just incorrect. Look at my answer to see it. EzraNehemiah_TF2 3552 — 9y
View all comments (5 more)
0
I put that into a local script and it didn't spawn? DeciusGalerius 20 — 9y
0
There, all fixed. If you've already established proper welds in the car, then the script should work fine if it's also placed in a localscript stored within a descendant of the player. It should also work faster than your original code, so I'd consider revising. aquathorn321 858 — 9y
0
The car wouldn't spawn before because I referenced "Anchor" wrong. Anchoring the car wasn't necessary, so I just removed it. aquathorn321 858 — 9y
0
How does using game.Players.LocalPlayer make the script run faster? For all we know, it's practically the same thing. Also, for getting services don't do "game.Players" do "game:GetService("Players")". Just a tip! EzraNehemiah_TF2 3552 — 9y
0
I never said it made the script run faster, it just cleaned up the code. What makes it run faster is removing the unecessary if statements. aquathorn321 858 — 9y

Answer this question