Simple script not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to detect when my projectile is touching the ground.

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    No topic talking about this issue. Tried multiple thing on my script, nothing change.

Hello! I’ve been trying to detect when my part is touching something, I now the .Touched thing, and its really simple to use, but somehow, it doesn’t work. There is 2 scripts where the part is modified.

This is the first script, inside the part

local part = script.Parent
part.Touched:Connect(function(hit)
	print("Touched")
end)

this is the function I call when I shoot the projectile. But nothing happens when my projectile is touching the floor
By the way, the sphere is the CLONE of the ball where the first script is.
‘sc’ is just a part to emit particle, and the for loop is what make the part moving.

local function shoot(s, m, f, sphere)
	local sc = Shooting:Clone()
	sc.Parent = workspace
	sc.CFrame = sphere.CFrame * CFrame.new(0, 0, 3)
	sc.Effects.Sparks:Emit(20)
	sc.ParticleEmitter:Emit(20)
	wait(.12)
	for i = 0, 75, 1 do
		local t = i/75
		local curve = lerp_quadratic(s.p,m.p,f,t)
		ts:Create(sphere, TweenInfo.new(.3), {Position = curve}):Play()
		task.wait(.001)	
	end
end
1 Like

You’re doing it wrong, when the part is touched you’re printing “Touched”, but you’re not calling shoot.

Move the code in shoot() inside the first code.

I see that you are cloning a part, which should have the first script inside it.

  1. Are you sure the script is inside the part?
  2. Why aren’t you just connecting the BasePart.Touched event when you clone the part initially?
  3. Can you provide some extra code, errors, or even a video of what is happening?

EDIT:

When asking a question, it’s important to add as much context as necessary, this includes but is not limited to:

  1. All pertinent code
  2. Any and all errors // output relating to the code
  3. Videos or images of what is actually happening
  4. An explanation of the expected behavior of the code.
1 Like
local RP = game:GetService("ReplicatedStorage")
local event = RP.Attack

local RockModule = require(game.ReplicatedStorage.Modules.RocksModule)

local function lerp_quadratic(p0, p1, p2, t)
	return (1 - t)^2 * p0 + 2 * (1 - t) * t * p1 + t^2 * p2
end

local ts = game:GetService("TweenService")

local Shooting = workspace.Shooting 

local function shoot(s, m, f, sphere)
	local sc = Shooting:Clone()
	sc.Parent = workspace
	sc.CFrame = sphere.CFrame * CFrame.new(0, 0, 3)
	sc.Effects.Sparks:Emit(20)
	sc.ParticleEmitter:Emit(20)
	wait(.12)
	for i = 0, 75, 1 do
		local t = i/75
		local curve = lerp_quadratic(s.p,m.p,f,t)
		ts:Create(sphere, TweenInfo.new(.3), {Position = curve}):Play()
		task.wait(.001)	
	end
end


event.OnServerEvent:Connect(function(player, humrp, mousePos, cam, partPos)
	local rp1 = RaycastParams.new()
	rp1.IgnoreWater = true
	rp1.FilterDescendantsInstances = {humrp.Parent:GetDescendants()}
	rp1.FilterType = Enum.RaycastFilterType.Exclude
	
	local player = game.Players:GetPlayerFromCharacter(humrp.Parent)	
	local start1 = humrp.CFrame * CFrame.new(0, 10, 0)
	local start2 = humrp.CFrame * CFrame.new(10, 5, 0)
	local start3 = humrp.CFrame * CFrame.new(-10, 5, 0)	
	local finish = (humrp.Position - Vector3.new(0, -5, 0)) + mousePos.lookVector * 150
	local mid1 = humrp.CFrame * CFrame.new(0,0,(humrp.CFrame.lookVector * 10)) * CFrame.new(0, 22, 0)	                                                   
	local mid2 = humrp.CFrame * CFrame.new(0,0,(humrp.CFrame.lookVector * 10)) * CFrame.new(15, 17, 0)	                                               
	local mid3 = humrp.CFrame * CFrame.new(0,0,(humrp.CFrame.lookVector * 10)) * CFrame.new(-15, 17, 0)	
	local BallAppear = workspace.BallAppear:Clone()
	BallAppear.Parent = workspace

	print(2)

	local sphere1 = workspace.Ball:Clone()
	sphere1.Parent = workspace.Clones
	sphere1.CanTouch = true
	sphere1.CanCollide = true
	local sphere2 = workspace.Ball:Clone()
	sphere2.Parent = workspace.Clones
	sphere2.CanTouch = true
	sphere2.CanCollide = true
	local sphere3 = workspace.Ball:Clone()
	sphere3.Parent = workspace.Clones
	sphere3.CanTouch = true
	sphere3.CanCollide = true
	
	sphere1.CFrame = start1
	BallAppear.CFrame = sphere1.CFrame
	BallAppear.Effects.Slash:Emit(200)
	BallAppear.Effects.Sparks:Emit(200)
	BallAppear.ParticleEmitter:Emit(200)
	wait(.7)

	
	sphere2.CFrame = start2
	BallAppear.CFrame = sphere2.CFrame
	BallAppear.Effects.Slash:Emit(200)
	BallAppear.Effects.Sparks:Emit(200)
	BallAppear.ParticleEmitter:Emit(200)
	wait(.7)
	
	sphere3.CFrame = start3
	BallAppear.CFrame = sphere3.CFrame
	BallAppear.Effects.Slash:Emit(200)
	BallAppear.Effects.Sparks:Emit(200)
	BallAppear.ParticleEmitter:Emit(200)
	wait(.7)

	
	BallAppear:Destroy()
	sphere1.Trail.Enabled = true
	sphere2.Trail.Enabled = true
	sphere3.Trail.Enabled = true
	
	task.spawn(shoot, start1, mid1, finish, sphere1)
	wait(.5)
	
	task.spawn(shoot, start2, mid2, finish, sphere2)
	wait(.5)

	task.spawn(shoot, start3, mid3, finish, sphere3)
	wait(.5)

end)

This is the full code,

here is a video, nothing in output

1 Like

btw what do you mean by connecting BasePart.Touched event when I clone the part

1 Like

What i mean is in your shoot function:

local function shoot(s, m, f, sphere)
	local sc = Shooting:Clone()
	sc.Parent = workspace
	sc.CFrame = sphere.CFrame * CFrame.new(0, 0, 3)
        Shooting.Touched:Connect(function(hit)
               print("Touched!")
        end)
	sc.Effects.Sparks:Emit(20)
	sc.ParticleEmitter:Emit(20)
	wait(.12)
	for i = 0, 75, 1 do
		local t = i/75
		local curve = lerp_quadratic(s.p,m.p,f,t)
		ts:Create(sphere, TweenInfo.new(.3), {Position = curve}):Play()
		task.wait(.001)	
	end
end

try this, disable the script inside the part and see if this helps at all.

2 Likes

nah, its not working, nothing in output. :confused:

1 Like

Are you firing event on the server?

1 Like

I’m firing it from a local script in a tool in starter pack.


local mouse = player:GetMouse()

local part = workspace:FindFirstChild("mouse"):Clone()

local tool = script.Parent

tool.Equipped:Connect(function()
	part.Parent = workspace.playerMouse
	part.Name = player.Name
	RunService.RenderStepped:Connect(function()
		mouse.TargetFilter = part
		part.Position = mouse.Hit.p
	end)
end)

tool.Unequipped:Connect(function()
	workspace.playerMouse:FindFirstChild(player.Name):Destroy()
end)

tool.Activated:Connect(function()
	local char = player.Character
	local humrp = char:FindFirstChild("HumanoidRootPart")
	event:FireServer(humrp, mouse.Hit, workspace.CurrentCamera.CFrame, part.Position)
end)

everything works well except that .Touched function.

1 Like

You can’t fire server events from a local script. Use a BindableEvent instead. These are for events on the same side, client to client, server to server.

I mean, think about if we could fire RemoteEvents from the client? Games would be ruined!

2 Likes

uh, maybe I did not explained myself correctly, but its client to server event, I fire it from a local script to a server script, like I often do, and everything works well, except one thing, this .Touched prolbem. Shoot function works fine, projectiles are going well…
I don’t understand why do you say it couldn’t work

1 Like

RemoteEvents CAN be fired from the client. That is quite literally the entire point of them. I highly suggest you re-read the documentation on this Instance.
Remote Events and Callbacks | Documentation - Roblox Creator Hub

RemoteEvents are used for communication from the client to the server and back.

Please do not spread misinformation or misleading content on the forums, it only serves to confuse the OP, and does not help in finding an actual solution to the problem at hand.

3 Likes

Alright I reviewed the documentation and discovered this important piece of information:

This event only fires as a result of physical movement, so it will not fire if the CFrame property was changed such that the part overlaps another part. This also means that at least one of the parts involved must not be Anchored at the time of the collision.

So you changing the CFrame essentially negates the event entirely and will not change. The .Touched event will only fire if the part is being acted upon by Roblox’s physics engine, not CFrame.

A way you can solve this issue is by firing a Raycast directly down, using the diameter of the sphere as its length, and if that raycast detects the ground (either part or terrain) then you can consider that a collision.

However this solution becomes even better (maybe) if you use Roblox’s new ShapeCast instance with the same diameter and shape of the part to detect collisions while manipulating the CFrame value.

Raycasting | Documentation - Roblox Creator Hub

WorldRoot | Documentation - Roblox Creator Hub

3 Likes

Thanks! My theory was the same as what you just send me, but I couldn’t find any proofs of what I thought.
I’ll try this shape cast instance.
Thank you for help! :smiley:

2 Likes

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