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

I want damage to happen to a player that touches this spike. Help?

I'm making a car with a spikes on the front of it, when I start my game. It gives me this error

"16:36:09.250 - Workspace.Minigun Truck.Spike.Damage:3: attempt to index field 'LocalPlayer' (a nil value)"

Code is here.


local Spike = script.Parent
local OnTouched = game.Players.LocalPlayer.Character.Humanoid:TakeDamage(30)

Spike.Touched:Connect(OnTouched)



1 answer

✓ Accepted answer

This might help:

local SpikePart = script.Parent
 
local function onPartTouch(otherPart)
	local partParent = otherPart.Parent
	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
	if humanoid then
		--Damage
		humanoid:TakeDamage(30)
	end
end
SpikePart.Touched:Connect(onPartTouch)

The problem is that unless you use localPlayer in a localScript, it probably won't work and I think that you can only connect functions to events, no variables.

1 comment

0

If this doesn't help you can go to this article for more help https://developer.roblox.com/en-us/articles/Creating-Traps-and-Pickups

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