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

How To Make A Script Only Target A Player That Has Touched A Block?

I want to make it so that if a player touches a block then a script targets a player for a players CFrame. Please help.

Thank you!

(script in serverscript service)

local function fireballChallenge()

	for i = 1,3 do

		for i,player in pairs(game.Players:GetPlayers()) do -- Targets every player but I want it to only target a player that has touched a block
			if player.Character then
				local fireball = game.ReplicatedStorage.Fireball:Clone()
				local fireball2 = game.ReplicatedStorage.Fireball2:Clone()
				local fireball3 = game.ReplicatedStorage.Fireball3:Clone()
				local fireball4 = game.ReplicatedStorage.Fireball4:Clone()
				local fireball5 = game.ReplicatedStorage.Fireball5:Clone()
				local target = game.ReplicatedStorage.Target:Clone()
				local target2 = game.ReplicatedStorage.Target2:Clone()
				local target3 = game.ReplicatedStorage.Target3:Clone()
				local target4 = game.ReplicatedStorage.Target4:Clone()
				local target5 = game.ReplicatedStorage.Target5:Clone()

				target.Parent = game.Workspace
				target.CFrame = (player.Character.HumanoidRootPart.CFrame - Vector3.new(0,3,0)) * CFrame.Angles(0,0,math.rad(90))
				target2.Parent = game.Workspace
				target2.CFrame = (player.Character.HumanoidRootPart.CFrame - Vector3.new(-20,3,0)) * CFrame.Angles(0,0,math.rad(90))
				target3.Parent = game.Workspace
				target3.CFrame = (player.Character.HumanoidRootPart.CFrame - Vector3.new(20,3,0)) * CFrame.Angles(0,0,math.rad(90))
				target4.Parent = game.Workspace
				target4.CFrame = (player.Character.HumanoidRootPart.CFrame - Vector3.new(0,3,20)) * CFrame.Angles(0,0,math.rad(90))
				target5.Parent = game.Workspace
				target5.CFrame = (player.Character.HumanoidRootPart.CFrame - Vector3.new(0,3,-20)) * CFrame.Angles(0,0,math.rad(90))

				wait(0.5)

				fireball.Parent = game.Workspace
				fireball.CFrame = target.CFrame + Vector3.new(0,30,0)
				fireball2.Parent = game.Workspace
				fireball2.CFrame = target.CFrame + Vector3.new(20,30,0)
				fireball3.Parent = game.Workspace
				fireball3.CFrame = target.CFrame + Vector3.new(-20,30,0)
				fireball4.Parent = game.Workspace
				fireball4.CFrame = target.CFrame + Vector3.new(0,30,20)
				fireball5.Parent = game.Workspace
				fireball5.CFrame = target.CFrame + Vector3.new(0,30,-20)

				wait(2)

				target:Destroy()
				target2:Destroy()
				target3:Destroy()
				target4:Destroy()
				target5:Destroy()
			end
		end
		wait(1)
	end
end

while wait(5) do
	
	local spike = game.ReplicatedStorage.Tools.Spikes:Clone()
	local spiketarget = game.ReplicatedStorage.Tools.SpikeTargets:Clone()
	
	spiketarget.Parent = game.Workspace
	wait(0.5)
	spike.Parent = game.Workspace

	
	wait(2)
	
	spike:Destroy()
	spiketarget:Destroy()
	
	fireballChallenge()

end

2 answers

✓ Accepted answer

You cannot do it directly thru the function but you can create an variable like

local function fireballChallenge(hit)

and at the end when you wanna call the part you do

game.Workspace.Path.To.Part.Touched:Connect(fireballChallenge())

and remove line 5 and 6 and replace it with

if hit.Parent:FindFirstChild("Humanoid") then
local player = hit.Parent

I followed what you said to do but it didn't work. Did I put it right?

local function fireballChallenge(hit)

	for i = 1,3 do

		if hit.Parent:FindFirstChild("Humanoid") then
			if game.Players:GetPlayerFromCharacter(hit.Parent) then
			local player = hit.Parent
				local fireball = game.ReplicatedStorage.Fireball:Clone()
				local fireball2 = game.ReplicatedStorage.Fireball2:Clone()
				local fireball3 = game.ReplicatedStorage.Fireball3:Clone()
				local fireball4 = game.ReplicatedStorage.Fireball4:Clone()
				local fireball5 = game.ReplicatedStorage.Fireball5:Clone()
				local target = game.ReplicatedStorage.Target:Clone()
				local target2 = game.ReplicatedStorage.Target2:Clone()
				local target3 = game.ReplicatedStorage.Target3:Clone()
				local target4 = game.ReplicatedStorage.Target4:Clone()
				local target5 = game.ReplicatedStorage.Target5:Clone()

				target.Parent = game.Workspace
				target.CFrame = (player.Character.HumanoidRootPart.CFrame - Vector3.new(0,3,0)) * CFrame.Angles(0,0,math.rad(90))
				target2.Parent = game.Workspace
				target2.CFrame = (player.Character.HumanoidRootPart.CFrame - Vector3.new(-20,3,0)) * CFrame.Angles(0,0,math.rad(90))
				target3.Parent = game.Workspace
				target3.CFrame = (player.Character.HumanoidRootPart.CFrame - Vector3.new(20,3,0)) * CFrame.Angles(0,0,math.rad(90))
				target4.Parent = game.Workspace
				target4.CFrame = (player.Character.HumanoidRootPart.CFrame - Vector3.new(0,3,20)) * CFrame.Angles(0,0,math.rad(90))
				target5.Parent = game.Workspace
				target5.CFrame = (player.Character.HumanoidRootPart.CFrame - Vector3.new(0,3,-20)) * CFrame.Angles(0,0,math.rad(90))

				wait(0.5)

				fireball.Parent = game.Workspace
				fireball.CFrame = target.CFrame + Vector3.new(0,30,0)
				fireball2.Parent = game.Workspace
				fireball2.CFrame = target.CFrame + Vector3.new(20,30,0)
				fireball3.Parent = game.Workspace
				fireball3.CFrame = target.CFrame + Vector3.new(-20,30,0)
				fireball4.Parent = game.Workspace
				fireball4.CFrame = target.CFrame + Vector3.new(0,30,20)
				fireball5.Parent = game.Workspace
				fireball5.CFrame = target.CFrame + Vector3.new(0,30,-20)

				wait(2)

				target:Destroy()
				target2:Destroy()
				target3:Destroy()
				target4:Destroy()
				target5:Destroy()
			end
		end
		wait(1)
end
end

while wait(5) do
	
	local spike = game.ReplicatedStorage.Tools.Spikes:Clone()
	local spiketarget = game.ReplicatedStorage.Tools.SpikeTargets:Clone()
	
	spiketarget.Parent = game.Workspace
	wait(0.5)
	spike.Parent = game.Workspace

	
	wait(2)
	
	spike:Destroy()
	spiketarget:Destroy()
	
	fireballChallenge()

end
game.Workspace.InvisTouch3.Touched:Connect(fireballChallenge())

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