How to make part collect other part

  1. What do you want to achieve?

Simple! I have a script that makes a circle around your character, and i want to make it so if the circle touches a part named “Coiner” inside workspace it destroys the part and adds 1 point to the DataStore

  1. What is the issue? I

I can’t seem to figure out how to get the player and part checked in the same script and add a point to it. I have many attempts at it and i gave my best shot at it now.

  1. What solutions have you tried so far?

Multiple tutorials, none helped.

Circle Script

local ring1 = Instance.new("Part", workspace)
ring1.Shape = "Cylinder"
ring1.Size = Vector3.new(0.15, 6, 6)
ring1.Name = "Ring"

local debounce = false

ring1.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if debounce == false then
		debounce = true
		local head = hit.Parent.HumanoidRootPart
		ring1.CanCollide = false
		ring1.Anchored = false
		local Scriptt = script.CfInelu:Clone()
		Scriptt.Parent = ring1
		Scriptt.Enabled = true
		local Weld = Instance.new("Weld", workspace)
		Weld.Part0 = head
		Weld.Part1 = ring1
		ring1.Rotation = Vector3.new(0,0,90)
			ring1.Position = head.Position - Vector3.new(0,3,0)
		end
	end
end)

The script that it is in the Circle itself

local Players = game:GetService("Players")

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local Player = Players:GetPlayerFromCharacter(hit.Parent)
		
		local IsPlayer = false
		
		if Player then
			IsPlayer = true
		end
		
		if hit.Name == "Coiner" and IsPlayer == true then
			print("Works") -- Dosen't print and does not work
			local rcoin = Player.leaderstats.Coins
			local brebirth = Player.leaderstats.Rebirths
			rcoin.Value += 1
		end
	end
end)


Any feedback would be appreciated!

Shouldn’t you have IsPlayer set to false at the beginning of the script and have the circle scriptbe:

local Players = game:GetService("Players")
local IsPlayer = false

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local Player = Players:GetPlayerFromCharacter(hit.Parent)		
        if Player then
			IsPlayer = true
    		if hit.Name == "Coiner" and IsPlayer == true then -- you may want to check this first instead
         		print("Works") -- Doesn't print and does not work
 	        	local rcoin = Player.leaderstats.Coins
    			local brebirth = Player.leaderstats.Rebirths
	    		rcoin.Value += 1
	    	end
		end
		local IsPlayer = false
end)

Still dosen’t work. I think i found why is it happening but i dont know how to put it into scripting terms. The problem that i think it is. Is the fact that If they are checking the player. (So that means that the functions continues only if the hit.Parent == Player )

And also the fact that i dont think it can hit 2 target’s at once

Try:

		local Player = Players:GetPlayerFromCharacter(hit.Parent)
        print(Player) --lets you know why or why not the next line will read true or false		
        if Player and and not IsPlayer then --forgot to use the debounce here instead of the next 'if' because you set IsPlayer to true in the following line.

Thank you for your time but i have figured it out. Have a good one

Please post how you solved the issue so if people search for a similar problem your Solution can help them out.

What i first did is Disconnect the function where the ring is created so it would be a one time thing.
Here is what the current script looks like

local Players = game:GetService("Players")

local ring1 = Instance.new("Part", workspace)
ring1.Shape = "Cylinder"
ring1.Name = "Ring"


local touchConnection = nil	--	Predefined to silence error of variable not existing within function
touchConnection = ring1.Touched:Connect(function(hit)
	print(hit.Name)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		ring1.Size = Vector3.new(0.15, player.SizeY, player.SizeZ)
		touchConnection:Disconnect()
		ring1.Parent = hit.Parent
		local head = hit.Parent.HumanoidRootPart
		ring1.CanCollide = false
		ring1.Anchored = false
		local Scriptt = script.CfInelu:Clone()
		Scriptt.Parent = ring1
		Scriptt.Enabled = true
		local Weld = Instance.new("Weld", ring1)
		Weld.Part0 = head
		Weld.Part1 = ring1
		ring1.Rotation = Vector3.new(0,0,90)
		ring1.Position = head.Position - Vector3.new(0,3,0)
	end
end)

And after that i have a script inside that is cloned into the circle around my char that is createad in the script above that is createad.
and in the script i get the Variables inside the leaderstats and i got the results needed.

local Players = game:GetService("Players")
local Player = Players:GetPlayerFromCharacter(script.Parent.Parent)
local LeaderStats = Player:FindFirstChild("leaderstats") or Player:WaitForChild("leaderstats", 10)
local Coins = LeaderStats:FindFirstChild("Coins") or LeaderStats:WaitForChild("Coins", 10)
local Rebirths = LeaderStats:FindFirstChild("Rebirths") or LeaderStats:WaitForChild("Rebirths", 10)
local RP = game:GetService("ReplicatedStorage")
local Values = RP:FindFirstChild("Values")

local coinspawn = Values:FindFirstChild("coinspawn")

function onPartTouched(part)
	--[[local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
	if humanoid then
		local player = Players:GetPlayerFromCharacter(humanoid.Parent)
		if player then
			local leaderstats = player:FindFirstChild("leaderstats")
			if leaderstats then
				local points = leaderstats:FindFirstChild("Points")
				if points then
					points.Value = points.Value + 1 -- Add 1 to the player's points
				end
			end
		end
	end]]--
	if part.Name == "Coiner" then
		part:Destroy() -- Destroy the "Coiner" part when touched
		coinspawn.Value -= 1
		Coins.Value += 1 * Rebirths.Value + 1
	end
end

script.Parent.Touched:Connect(onPartTouched) 

If anyone wants more details on the subject i am free at any time
Cheers

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.