Basic Touched event questions and script help

Hello,

im trying to make a part spawner thats spawns parts on my baseplate.
i want it so when the player clicks OR walks on brick it gives them 1 coin.

so far the script is good and everything has been working but when i tried to add the “walk on” part of the script things started failing.

NOTE: There shouldnt be much scripting needed, as i probably just messed up format. i have tried looking for the issue for 10-30 minutes and i cant find it.

Script

local tweenService = game:GetService("TweenService") 
local replicatedStorage = game:GetService("ReplicatedStorage") 
local sound = game.Workspace.Sound
while wait() do 
	wait(1)
	script.Parent.Text = 'loading... 1 '
	wait(1)
	script.Parent.Text = 'loading... 2 '
	wait(1)
	script.Parent.Text = 'loading... 3 '

	script.Parent.Text = 'error loading '

	local Part = Instance.new("Part")
	Part.Parent = game.Workspace
	Part.BrickColor = BrickColor.Random()
	Part.Material = 'Metal'
	local A = math.random(-500,500)
	local B = 0
	local C = math.random(-500,500)
	Part.Position =  Vector3.new(A, B, C)

	script.Parent.Text = tostring(Part.Position)
	local tweeningInformation = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 1, false)
	local partProperties = { 
		Size = Vector3.new(0,0,0),
		Color = Color3.new(1, 0, 0),
		Transparency = 1}
	local Tween = tweenService:Create(Part, tweeningInformation, partProperties)
	local e = false
	local clickDetector = Instance.new("ClickDetector")
	clickDetector.Parent = Part
	local debounce = false
	clickDetector.MouseClick:Connect(function(player)
		e = true
		local touched = Part
		touched.Touched:Connect(function(invisible)
			e = true
			if not e then
		if not debounce then
				debounce = true
				script.Parent.Text = (player.Name.. " clicked the brick")
				sound:Play()
				Tween:Play()
				game:GetService("ReplicatedStorage"):FindFirstChild("updateCash"):FireServer()
				wait(0.3)
				Part:Destroy()
			debounce = false
				end
			end
		end)
	end)
end

Note: it spawns parts but clicking them or walking on them does nothing.

this may help
its a local script


Thank You For The Help I Hope Your Able To Fix The Issue
to be honest im not really sure what to do when it comes to making it so if players do one or the other it triggers the same thing.

You put the Touched event (and code body) inside the MouseClick code body. You’ll wanna have it be outside the code body of the MouseClick for it to work as intended.

local hasClicked = false
local hasTouched = false
clickDetector.MouseClick:Connect(function(player)
	if not hasClicked and not hasTouched then
		hasClicked = true
		script.Parent.Text = player.DisplayName .. " clicked the brick"
		sound:Play()
		Tween:Play()
		wait(0.3)
		Part:Destroy()
	end
end)
Part.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player and not hasTouched and not hasClicked then
		hasTouched = true
		script.Parent.Text = player.DisplayName .. " clicked the brick!"
		sound:Play()
		Tween:Play()
		wait(0.3)
		Part:Destroy()
	end
end)

Something like this.

Please don’t do this - it’s bad practice and is unnecessary, you already have 3 wait(1) in your while loop. Just use while true do, instead of using a value that’s considered truthy by Lua (anything not false or nil).

1 Like

ok so is this good?

local tweenService = game:GetService("TweenService") 
local replicatedStorage = game:GetService("ReplicatedStorage") 
local sound = game.Workspace.Sound
while wait() do 
	wait(1)
	script.Parent.Text = 'loading... 1 '
	wait(1)
	script.Parent.Text = 'loading... 2 '
	wait(1)
	script.Parent.Text = 'loading... 3 '

	script.Parent.Text = 'error loading '

	local Part = Instance.new("Part")
	Part.Parent = game.Workspace
	Part.BrickColor = BrickColor.Random()
	Part.Material = 'Metal'
	local A = math.random(-500,500)
	local B = 0
	local C = math.random(-500,500)
	Part.Position =  Vector3.new(A, B, C)

	script.Parent.Text = tostring(Part.Position)
	local tweeningInformation = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 1, false)
	local partProperties = { 
		Size = Vector3.new(0,0,0),
		Color = Color3.new(1, 0, 0),
		Transparency = 1}
	local Tween = tweenService:Create(Part, tweeningInformation, partProperties)
	local e = false
	local clickDetector = Instance.new("ClickDetector")
	clickDetector.Parent = Part
	local debounce = false
	local hasClicked = false
local hasTouched = false
clickDetector.MouseClick:Connect(function(player)
	if not hasClicked and not hasTouched then
		hasClicked = true
		script.Parent.Text = player.DisplayName .. " clicked the brick"
		sound:Play()
		Tween:Play()
		wait(0.3)
		Part:Destroy()
	end
end)
Part.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player and not hasTouched and not hasClicked then
		hasTouched = true
		script.Parent.Text = player.DisplayName .. " clicked the brick!"
		sound:Play()
		Tween:Play()
		wait(0.3)
		Part:Destroy()
	end
end)
1 Like