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

Tool that should spawn a part on click doesnt work????

So as you can see from the title, I am trying to make a tool that spawns a part. I have made it so it cant be spammed crashing the server in under a second but it just wont spawn anything . Here u have my code

 local JustSpawned = false   
 local Tool = game.StarterPack.SandTool
 local Player = game:GetService("Players").LocalPlayer;
 local Mouse = Player:GetMouse();
 
 function draw() 
 	local Sand = Instance.new("Part")
 	Sand.Anchored = false
 	Sand.Size = Vector3.new(0.1,0.1,0.1)
 	Sand.BrickColor = BrickColor.new("Sand yellow")
 	JustSpawned = true
 	wait(1)
 	JustSpawned = false
 end
 if Tool.Equipped then
	if JustSpawned == false then
 		Mouse.Button1Down:connect(function() draw() end)
	end
 end

4 comments

0

I don't even know if I'm right, actually I 99% doubting myself, but you are trying to spawn a part for everyone from a local script. I don't think it works like that. But I'm probably wrong.

0

Also what are you trying to spawn? Maybe I can give you a better way of doing it.

0

Im trying to spawn a little cube which is unanchored and is kinda like a sand particle

0

What type of script - is it a sever or is it a client? Anything on the output? Has the Sand been parented to workspace or somewhere else? Is it supposed to spawn for all players or is it for the player that holds the tool only? (Since it was kinda unclear)

1 answer

I found a few issues:

  1. You didn't parent the Sand

  2. Few errors

  3. A better syntax could be used

Let's start with the first issue: Parenting

  • You need to parent the Sand to the workspace or a descendant of the workspace in order to make it visible.

  • It can be garbage-collected if not parented.

Errors

  • The position wasn't specified

  • Anything on the StarterPack would get automatically re-parented into the player client's Backpack

Syntax

  • Tools already has their own function if the mouse clicks while tools are equipped: Activated

  • if var == false can be replace with not

  • task.wait(n)/task.wait() are advised to use than wait(n)/wait(0

Here is the fixed script: (Assuming it's a Client Script)

local JustSpawned = false   
local Tool = script.Parent

local plr = game:GetService("Players").LocalPlayer
local Character = plr.Character or plr.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart") -- // This is if it's R15

function draw() 
	local Sand = Instance.new("Part")
	Sand.Anchored = false
	
	Sand.Size = Vector3.new(0.1,0.1,0.1)
	Sand.Position = Vector3.new(math.random(HumanoidRootPart.Position.X, HumanoidRootPart.Position.X + 20), HumanoidRootPart.Position.Y + 25, math.random(HumanoidRootPart.Position.Z, HumanoidRootPart.Position.Z + 20))
	
	Sand.BrickColor = BrickColor.new("Sand yellow")
	Sand.Parent = workspace
	
	JustSpawned = true
	task.wait(1)
	JustSpawned = false
end

Tool.Activated:Connect(function()
	if not JustSpawned then
		draw()
	end
end)

1 comment

0

Your Solution was pretty helpfull at achiving my almost final verison. What I wanted to do now Is sort the sand into a folder that is inside a general folder on what the player spawned.

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