How could I get this to work?

Hey!

I was curious of how I could get the following to work:

As you see, I want it so once the Gray part touches the black part, you’ll get cash.

Any idea?

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		local random = math.random(1, 20)
		
		print(random)
		
		if hit.Parent.Name == "MoneyPart" then
			plr.leaderstats.Money.Value += random 
			hit.Parent:Destroy()
		end
		
	end
end)

Thanks

5 Likes

Well first you should remove the hit.Parent on the part that destroys. like just hit:Destroy() not hit.Parent

3 Likes

Is the part that your dragging a descendant of your character?

2 Likes

I guess so.

I could try doing hit:Destroy() too, any other tips?

2 Likes

try this:

local blackPart = nil-- put that black part in this variable

local debounce = true

blackPart.Touched:Connect(function(hit)
	if hit.Name == "MoneyPart" then
		if debounce then
			local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
			if plr then
				plr.leaderstats.Money.Value += math.random(1,20)
				hit:Destroy()
				debounce = false
				wait(1) -- Cool down time
				debounce = true
			end
		end
	end
end)

Sadly didn’t work. Too bad I’m not receiving errors in the output.

Is the part your dragging parent the players character?

And is the name of the part “MoneyPart”

I’m unsure. For testing I got this dragging script off of youtube from a lumber tycoon video.

If you need the script:
startercharacterscripts localscript

local CAS = game:GetService("ContextActionService")

local GrabObject = nil
local GrabStart = false
local Dragger = nil

local player = game.Players.LocalPlayer
local character = player.Character
local mouse = player:GetMouse()
local camera = game.Workspace.CurrentCamera

function Grab(actionName, UserInputState, InputObject)
	if actionName == "Grab" then
		if UserInputState == Enum.UserInputState.Begin then
			
			local Magnitude = (mouse.Hit.Position - character.Head.CFrame.Position).magnitude
			if Magnitude < 10 then

				if mouse.Target then

					GrabObject = mouse.Target
					GrabStart = true

					local DragBall = createDragBall()
					DragBall.CFrame = mouse.Hit
					Dragger = DragBall

					mouse.TargetFilter = GrabObject

					local DragBallWeld = weldBetween(DragBall,GrabObject)
					addMover(DragBall)

					while Dragger do
						local cf = CFrame.new(character.Head.Position, mouse.Hit.Position)
						Dragger.Mover.Position = (cf + (cf.LookVector * 10)).Position
						Dragger.RotMover.CFrame = camera.CFrame * CFrame.Angles(Dragger.RotOffset.Value.X,Dragger.RotOffset.Value.Y, Dragger.RotOffset.Value.Z)
						wait()
					end
					mouse.TargetFilter = nil
				end
			end
		elseif UserInputState == Enum.UserInputState.End then
			GrabObject = nil
			GrabStart = false
			if Dragger then
				Dragger:Destroy()
				Dragger = nil
			end
		end
	end	
end


function weldBetween(a, b)
	local weld = Instance.new("ManualWeld", a)
	weld.C0 = a.CFrame:inverse() * b.CFrame
	weld.Part0 = a
	weld.Part1 = b
	return weld
end


function addMover(part)
	local newMover = Instance.new("BodyPosition")
	newMover.Parent = part
	newMover.MaxForce = Vector3.new(40000,40000,40000)
	newMover.P = 40000
	newMover.D = 1000
	newMover.Position = part.Position
	newMover.Name = "Mover"

	local newRot = Instance.new("BodyGyro")
	newRot.Parent = part
	newRot.MaxTorque = Vector3.new(3000,3000,3000)
	newRot.P = 3000
	newRot.D = 500
	newRot.CFrame = game.Workspace.CurrentCamera.CFrame
	newRot.Name = "RotMover"

	local RotOffset = Instance.new("CFrameValue")
	RotOffset.Name = "RotOffset"
	RotOffset.Parent = part
end


function createDragBall()
	local DragBall = Instance.new("Part")
	DragBall.BrickColor = BrickColor.new("Electric blue")
	DragBall.Material = Enum.Material.Wood
	DragBall.Size = Vector3.new(.2,.2,.2)
	DragBall.Shape = "Ball"
	DragBall.Name = "DragBall"
	DragBall.Parent = workspace
	DragBall.Transparency = 1
	return DragBall
end


CAS:BindAction("Grab", Grab, false, Enum.UserInputType.MouseButton1)

Definently. I made sure that’s 100% correct.

did you make sure to set the black part variable?

yup, i did. I changed the “nil” to the game.Workspace.Part (No other parts are named “Part”, dont worry)

I’m not quite sure if this will work, but here we go:

First, we will create a variable for the GreyPart, so it can be detected.

-- Variable for the Grey Part
local GreyPart = game.Workspace.GreyPart -- Change this to the correct location

Now, I see you have your script inside of the black part. This means:

-- Variables
local GreyPart = game.Workspace.GreyPart -- Change this to the correct location
local BlackPart = script.Parent

Now, the function: if you touch ‘BlackPart’ something will happen, so:

BlackPart.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

Check if it is GreyPart:

if hit.Parent = GreyPart then
plr.leaderstats.Money.Value += random
hit.Parent:Destroy()

end
end
end)

So, the full script would be:

-- Variables
local GreyPart = game.Workspace.GreyPart -- Change this to the correct location
local BlackPart = script.Parent

-- Function
BlackPart.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

if hit.Parent = GreyPart then
plr.leaderstats.Money.Value += random
hit.Parent:Destroy()

end
end
end)

I hope this is what you were looking for.

This is very weird. It’s not working and I still get no errors?

Oh, that is strange indeed. What part is not working? Maybe add some prints to see what part of the script doesn’t work.

when your dragging, you should name the part the name of the player. Then you can get the player that is dragging the part by going game.players[hit.Name]. then check if its not nil.

I see, I will change my script soon; forgot some important things.

Hi @NoobiDerpieDev,

I recently posted a script on your topic which didn’t work. I decided to make the script again so it will work for you, so you can build your game further! I will also explain how I created the script so you understand what the script does.


I will first start with creating the variables for the GreyPart and the BlackPart, so you can check later if it is actually the GreyPart touching the BlackPart, and not something/someone else.

  • The normal script is inside of the GreyPart.
  • Change the BlackPart to the correct location
local GreyPart = script.Parent
local BlackPart = game.Workspace.Conveyorthing

I see you wanted a cooldown for each time the BlackPart is touched by the GreyPart, so we need variables for that, too.

  • The Cooldown is set to false. This means the Cooldown is not active at this time.
  • The CooldownTime can be changed to how long you want the Cooldown to last.
local Cooldown = false
local CooldownTime = 10

The player seems to get a random amount of money from 1-20 each time a GreyPart is detected.

local Random = math.random(1,20)

According to you, the part is named after the owner. I want to make a variable for the name of the part, which is the username of the owner of the part.

local Username = GreyPart.Name

We have set the main variables and now we can create the function. What makes the script activate? The touched event.

  • Every time something or someone touches the BlackPart, the script gets activated.
Blackpart.Touched:Connect(function(hit)

But you only want to reward the player with money if ‘GreyPart’ touches it. This line will only make the script go on if the something/someone who touched it is GreyPart.

  • hit.Name is the name of something or someone who touches it.
  • Username refers to the name of the GreyPart.
if hit.Name = Username then

But we still may not reward the player, because we need to check if the Cooldown is over.

  • This line stands for ‘If there is no Cooldown (so Cooldown = false), then…’
if not Cooldown then

So now we see that GreyPart touched BlackPart, and the Cooldown is over so we may now reward the player. But how do we know who the player is? We will make a player variable.

  • So we go to the list of all players on the server and we use FindFirstChild. Username refers to the name of the GreyPart and the name of the GreyPart is the owner of that part. That is the player.
local Player = game.Players:FindFirstChild(Username)

Most games have a folder named ‘leaderstats’ with all leaderstats inside of it. You want to reward the player with ‘Money’. The Money is inside of the folder, so we need a variable for the folder.

  • Please note: the folder in my game is called Leaderstats. Your folder may be called different, please check that. It is also case-sensitive.
local Folder = Player:WaitForChild("Leaderstats")

And the last variable is the Money, the Money is inside of the folder we made a variable of.

  • Please note: Money is case-sensitive, change the line if needed.
local Money = Folder:WaitForChild("Money")	

Now we will reward the money. Money has a different Value for every player.

  • So the Value of the Money of the player + Random (random is the variable we made)
Money.Value = Money.Value + Random

Done, the player received the Money. The last thing we want to do is activate the Cooldown.

  • We activate the Cooldown by setting it to true. We made a variable of CooldownTime which means the Cooldown lasts in my case for 10 seconds. Then, the Cooldown is over and is set to false again. The player is now able to get Money again.
Cooldown = true
wait(CooldownTime)
Cooldown = false

The script works in my game, I tested it. I’m not a very good scripter so there may be some things to improve. I hope it helped you, so you understand how to create a script like this. As I said, I’m not very good so this was a great opportunity to expand my knowledge too.


Edit: I don’t know if you want to destroy the part if the player received the money. That is not included in this script, sorry. If you need help with that, let me know.


Here is the script again but then in the entirety.

-- Variables

local GreyPart = script.Parent
local BlackPart = game.Workspace.Conveyorthing

local Cooldown = false
local CooldownTime = 10

local Random = math.random(1,20)
local Username = GreyPart.Name

-- Function

BlackPart.Touched:Connect(function(hit)
	
	if hit.Name == Username then
		if not Cooldown then
			
		local Player = game.Players:FindFirstChild(Username)
		local Folder = Player:WaitForChild("leaderstats")
		local Money = Folder:WaitForChild("Money")	
			
		Money.Value = Money.Value + Random
			
			Cooldown = true
			wait(CooldownTime)
			Cooldown = false
			
		end
	end
end)
1 Like

Hey, thank you so much for helping me.

I got one more problem:

Line 21:

local Folder = Player:WaitForChild("leaderstats")

Could you help? :slight_smile:

It seems that "Leaderstats" isn’t capitalized.(although it does seem to work, it’s a variable that isn’t used I think.)

Whoops, I’m in the wrong, actually, he meant to tell you locate the folder to that’s similar to that