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

How to fix "attempt to index nil with 'Touched'"?

I have a local script that activates a server script inside of itself using a remote event then Replicates apart from the Replicated storage and welds it to the player's hand and when it is touched by a humanoid it prints something but it keeps giving me an error like this "attempt to index nil with 'Touched'"

here is the entire server script:

Damage = 50
Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://4952995121"

script.Parent.First.OnServerEvent:Connect(function(Player)	
	local Rasengan = game.ReplicatedStorage.Rasengan
	RasenClone = Rasengan:Clone()
	RasenClone.Parent = workspace
	RasenClone.CanCollide = false
	
		
	playanim = Player.Character.Humanoid:LoadAnimation(Animation)
	playanim:Play()
	
	RasenClone.Anchored = false
	local weld = Instance.new("Weld", RasenClone)
	weld.Part1 = RasenClone
	weld.Part0 = Player.Character.RightHand
	weld.C0 = CFrame.new(0, -1.5, 0)
	
	for i = 1, 28 do
		RasenClone.Size = RasenClone.Size + Vector3.new(0.05, 0.05, 0.05)
		wait(0.1)
	end
	
	MainPart = RasenClone
	MainPlayer = Player
end)

script.Parent.Second.OnServerEvent:Connect(function(player, Damage)
	MainPart:Destroy()
	playanim:Stop()
end)

RasenClone.Touched:Connect(function(hit)
	if hit.Parent.Humanoid then
		MainPlayer.Character.Humanoid.WalkSpeed = MainPlayer.Character.Humanoid.WalkSpeed-16
		RasenClone:Destroy()
	end
end)

Thank You in advance

1 comment

0

It still says "attempt to index nil with 'Touched'"

3 answers

RasenClone is nil. Specify where it is. Like that:

workspace.RasenClone.Touched:Connect(function(hit)
    if hit.Parent.Humanoid then
        MainPlayer.Character.Humanoid.WalkSpeed = MainPlayer.Character.Humanoid.WalkSpeed-16
        RasenClone:Destroy()
    end
end)

Accept the answer if it helped you!

1 comment

0

it still says "attempt to index nil with 'Touched'"

ok on my THEORY u got a big error on ur code, so

script.Parent.First.OnServerEvent:Connect(function(Player)  
    local Rasengan = game.ReplicatedStorage.Rasengan
    RasenClone = Rasengan:Clone() -- u r cloning remoteEvent and not an obj
    RasenClone.Parent = workspace
    RasenClone.CanCollide = false --no cancollide on remoteevent

and this too

   RasenClone.Anchored = false -- u cant anchored remoteevent
    local weld = Instance.new("Weld", RasenClone) -- cant add instance new on remoteevent
    weld.Part1 = RasenClone
    weld.Part0 = Player.Character.RightHand
    weld.C0 = CFrame.new(0, -1.5, 0)

and ok the main problem is

RasenClone.Touched:Connect(function(hit)
    if hit.Parent.Humanoid then
        MainPlayer.Character.Humanoid.WalkSpeed = MainPlayer.Character.Humanoid.WalkSpeed-16
        RasenClone:Destroy()
    end
end)

the script wont know wat is RasenClone is, its because u're puting it at the wrong spot and using the remoteevent, not part obj. so be it

ok this is how i can tell u bcs im just a noob scripter, and i cant speak english well, and sorry if my theory are wrongs

Events are asynchronous. They do not yield the main thread, meaning that way before RasenClone will ever be assigned, it will still try to assign Touched after it.

Try moving it inside of the OnServerEvent connection. Note that nesting events are only okay if the nested event does not reconnect.

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