This script is inside a rock, and when you touch it without a ForceField, it's supposed to take 100 damage away. When you touch it and you DO have a ForceField, it's supposed to destroy it's self and have a 25% chance of giving you 100 points. If the rock doesn't touch a person, but it touches grass or a different rock, it's supposed to have a 33% chance of staying on the ground.
debounce = false script.Parent.Touched:connect(function(hit) if not hit then print 'gg roblox' return true elseif not hit.Parent then print 'gg roblox' return true end local h = hit.Parent:FindFirstChild("Humanoid") local ff = hit.Parent:FindFirstChild("ForceField") local p = hit.Parent:FindFirstChild("Grass") local p2 = hit.Parent:findFirstChild("Rock") local rl = hit.Parent:FindFirstChild("RocksLeft") if h then print 'humanoid found' if ff then print 'ff found' print (hit.Parent)--Player local points = game.Players:GetPlayerFromCharacter(hit.Parent):WaitForChild("Points") if rl.Value ~= 1 then rl.Value = rl.Value - 1 else rl.Value = 0 ff:Destroy() end local var1 = 1 local var2 = math.random(1,4) if var2 == var1 then points.Value = points.Value + 100 end script.Parent:Destroy() else if debounce == false then debounce = true h.Health = h.Health - 100 print 'rip' script.Parent:Destroy() end wait(.5) debounce = false end elseif p or p2 then local var = math.random(1,3) if var == 1 then script.Parent:Destroy() end end end)
The problem is that when you do touch it with a ForceField, it says:
attempt to index local 'rl' (a nil value)
Even though I'm asking for a IntValue named "RocksLeft"
Another thing is, when the rock hits the ground it doesn't destroy. None of them do. Even though there are a lot spawning each second
while true do wait(.05) clone = game.ServerStorage.GameRock:Clone() clone.Parent = workspace clone.Position = Vector3.new(math.random(-255,255),math.random(150,200),math.random(-255,255)) end
I didn't read through everything, but i do know this: You're using variables wrong.
local rl = hit.Parent:WaitForChild("RocksLeft") . . . hit.Parent.rl.Value
The line hit.Parent.rl
is looking for a child or a property named "rl" under hit.Parent
. But there isn't a child nor is there a property named "rl", so you get an error.
You already have a variable equal to the object, so just use it:
rl.Value
You would also probably want to do a check making sure that it's an actual Player Character that stepped on the brick, otherwise WaitForChild
will wait forever.
You get the new error because FindFirstChild
either returns the object you're looking for, or it returns nil
. If it returns nil in your case, the code will break because you are essentially saying
nil.Value
which is obviously flawed.
Keep using WaitForChild
, but use it after you make sure the Humanoid exists. This will make sure that your code doesn't wait forever, assuming all player characters have "RocksLeft" in them.
Perci's answer is correct about one issue, but there might be another problem here.
Line 14: local rl = hit.Parent:WaitForChild("RocksLeft")
The WaitForChild function will wait forever until hit.Parent gets a child object named "RocksLeft." This means that when your object is touched and fires your listener function, it will always freeze at line 14 if there is no RocksLeft object. This is fine if you only are dealing with Touched events from objects that have a RocksLeft object, but I'm guessing you also want to deal with other objects too.