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

Help with my placement system. No errors pop up?

I made a placement system for my game and it won't work. No errors come up. Help please? (P.S, this is all inside one handler script.)

local items = script.Parent.BuyItems.ShopButton.Frame:GetChildren()
local viewitems = game.ReplicatedStorage.Items:GetChildren()
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
for i,v in pairs(items) do
	if v:IsA("TextButton") then
		local name = v.Name
		v.MouseButton1Click:Connect(function()
			local cash = plr:WaitForChild("leaderstats").Cash
			if cash then
				cash.Value = cash.Value - v.Cost.Value
				for x,z in pairs(viewitems) do
					if z:IsA("Model") then
						while wait() do
							if mouse.Target == workspace:FindFirstChild(plr.CurrentBase.Value) then
								if z.Name == name then
									z:SetPrimaryPartCFrame(mouse.Hit)
								end
							end
						end
					end
				end
			end
		end)
	end
end

2 comments

0

What is plr.CurrentBase.Value? Do your models have a PrimaryPart set?

0

CurrentBase is the part that the player can place objects on. And yes. I have a primary part set. It's the hitbox for it.

1 answer

I can't really diagnose the issue without seeing your game hierarchy; that said, I can try and provide some sample code:

local items = script.Parent.BuyItems.ShopButton.Frame:GetChildren() -- an array of items
local viewitems = game.ReplicatedStorage.Items:GetChildren() -- an array of items
local player = game.Players.LocalPlayer -- the local Player Object
local mouse = player:GetMouse(); -- the local mouse
local inputservice = game:GetService("UserInputService");

for a, x in pairs(items) do
	if(x.ClassName == "TextButton")then
		x.InputBegan:connect(function(input)
			if(input.UserInputType == Enum.UserInputType.MouseButton1)then
				local placed = false;
				local cash = player.leaderstats.Cash;
				if(cash.Value > x.Cost.Value)then
					-- if we have enough money, then take that money away
					cash.Value = cash.Value - x.Cost.Value;
					-- clone the model whose name is the same as the button's name
					local clone = viewitems[x.Name]:Clone();
					clone.Parent == game.Workspace;
					-- now you'll have to update the model until a player places it
					while not placed do
						if(mouse.Target == player.CurrentBase.Value)then
							clone:SetPrimaryPartCFrame(mouse.Hit);
							local place;
							place = inputservice.InputBegan:connect(function(input)
								if(input.UserInputType == Enum.UserInputType.MouseButton1)then
									placed = true;
									place:disconnect()
								end
							end
						end
					end
				end
			end
		end)
	end
end

This is by no means perfect, as I don't know how your game works. Also needs a few things like checks as to whether a user is placing an item before they're allowed to place another, etc. Just a rough sketch of what it could look like.

I also took the liberty of using game.UserInputService, rather than the older Mouse events

4 comments

0

At line 17 it tries in index a nil value. Help?

0

If that's the case, then there is not a model with the same name as the button in ReplicatedStorage.Items

0

I fixed it. All i did was remove the get children:() on it and it fixed itself. but the input area near the placed = true and place:disconnect() area freezes studio and i have to kill the script.

0

Woops, you're right. I wasn't paying attention and tried indexing the array. Likewise, I wasn't paying attention, and you should add a wait() into the while not placed do loop.

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