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

My placement system is not working properly, no errors?

Asked by 6 years ago
Edited 6 years ago

I have a placement system for a defending base game. Players have a shop GUI to buy things to defend like parts and fences. There is a problem, though. When you click the item, it registers that you bought the product and puts it in the Workspace. But it goes off the screen and you can only see it for a split second before the product disappears. This program has NO errors, just my error ????

You can find the object tree here.

Here is the code so far:

for i,v in pairs(script.Parent:GetChildren()) do
    if v.ClassName == "Frame" then
        v:FindFirstChildWhichIsA("ImageButton").MouseButton1Up:connect(function()
            if script.Parent.Parent.Parent.Parent.leaderstats.Money.Value > tonumber(string.sub(v.PriceLabel.Text, 2, string.len(v.PriceLabel.Text))) then
                local clone = game.ServerStorage[v:FindFirstChildWhichIsA("ImageButton").Name]:Clone()
                clone.Parent = game.Workspace
                local highlight = Instance.new("SelectionBox")
                highlight.Parent = clone
                highlight.Color3 = Color3.new(0, 255, 0)
                local mouse = script.Parent.Parent.Parent.Parent:GetMouse()
                while wait() do
                    clone:MoveTo(Vector3.new(math.floor(mouse.X), 3, mouse.Y))
                end
                -- Also I need more code to detect when the player clicks, it places the item down and takes money away. That would go here.
                script.Parent.Parent.Parent.Parent.leaderstats.Money.Value = script.Parent.Parent.Parent.Parent.leaderstats.Money.Value - tonumber(string.sub(v.PriceLabel.Text, 2, string.len(v.PriceLabel.Text)))
            end
        end)
    end
end

I know this might not be the best way to accomplish this (I have not done this before!), but can anyone help me?

Thanks!

EDIT 1

Please paste the code into a code text editor if you have one, as this site cannot fit the long lines of code.

EDIT 2

Here is a simplified version with variables. You might still need to use the "view source" option to read the code better.

for i,v in pairs(script.Parent:GetChildren()) do
    if v.ClassName == "Frame" then
        v:FindFirstChildWhichIsA("ImageButton").MouseButton1Up:connect(function()
            local money = script.Parent.Parent.Parent.Parent.leaderstats.Money
            local price = tonumber(string.sub(v.PriceLabel.Text, 2, string.len(v.PriceLabel.Text)))
            if money.Value >= price then
                local clone = game.ServerStorage[v:FindFirstChildWhichIsA("ImageButton").Name]:Clone()
                clone.Parent = game.Workspace
                local highlight = Instance.new("SelectionBox")
                highlight.Parent = clone
                highlight.Color3 = Color3.new(0, 255, 0)
                local mouse = script.Parent.Parent.Parent.Parent:GetMouse()
                while wait() do
                    clone:MoveTo(Vector3.new(math.floor(mouse.X), 3, mouse.Y))
                end
                -- Also I need more code to detect when the player clicks, it places the item down and takes money away. That would go here.
                money.Value = money.Value - price
            end
        end)
    end
end
1
If your lines of code are thing long, it might be worth saving a few instance paths in variables to shorten your code and make it more readable. fredfishy 833 — 6y
0
Good idea. Might as well do that but make sure it still does the same thing. Pixelated_MC 33 — 6y
0
I made another version, try this. Pixelated_MC 33 — 6y
0
I think the problem could be that Vector3 is world space, so you're essentially setting the model's primary part 3 studs above the origin(0,0,0) in worldspace, and an X amount on the X-axis. X being whatever your mouse's current X coordinate is set to. jackfrost178 242 — 6y
View all comments (4 more)
0
I had known that, but there isn't a workaround because on your screen, there is an X-axis and a Y-axis but no Z-axis. Pixelated_MC 33 — 6y
1
mouse.Hit.p actually returns a Vector3, so you could just set the model's position to that. And maybe add Vector3.new(0,3,0) since I see you're trying to offset it. jackfrost178 242 — 6y
0
jackfrost178, post that as an answer and I will accept it. Pixelated_MC 33 — 6y
0
That's ok, I don't like posting answers unless I have a lot to say about something, but you're welcome if I helped you out. jackfrost178 242 — 6y

Answer this question