Hello, I am trying to make a script detect when it was hit by a tool that is swinging/activated The tool changes a value called "active" to 1 when swinging and back to zero when done. That part seems to be working. There is a comment showing where the script does not run past, yet I do not get an error.
game:WaitForChild("Players") local folder = script.Parent local toolName = "realTool" local currency = "DNA" local start = true local function collectitem(obj, otherpart) print("we got here") local player = game.Players:FindFirstChild(otherpart.Parent.Name) print("here too") if player == otherpart.Parent.Name then print("wow") -- script doesnt go here, it just stops and I cant figure out why if player.Character:FindFirstChild(toolName) then print("got this far") local amount = obj.Amount.Value obj:Destroy() player.leaderstats[currency].Value += amount end end end for _, obj in pairs(folder:GetChildren()) do if obj:IsA("Model") and obj:FindFirstChild("hit") then obj.hit.Touched:Connect(function(otherpart) if otherpart.Parent.active.Value == 1 then collectitem(obj, otherpart) print("plant cut") print(otherpart.Parent.active.Value) else print("plant not cut") print(otherpart) print(otherpart.Parent.active.Value) end end) end end
In this line you are comparing a player object to a string.
if player == otherpart.Parent.Name then
You probably want player.Name
instead.
Edit: I mocked up your setup in studio just now and all I really had to change to get it to work was the above. This is what I have:
game:WaitForChild("Players") local folder = script.Parent local toolName = "realTool" local currency = "DNA" local start = true local function collectitem(obj, otherpart) print("we got here") local player = game.Players:FindFirstChild(otherpart.Parent.Name) print("here too") if player.Name == otherpart.Parent.Name then print("wow") -- script doesnt go here, it just stops and I cant figure out why if player.Character:FindFirstChild("Tool") then print("got this far") end end end for _, obj in pairs(folder:GetChildren()) do if obj:IsA("Model") and obj:FindFirstChild("hit") then obj.hit.Touched:Connect(function(otherpart) if otherpart.Parent.active.Value == 1 then collectitem(obj, otherpart) print("plant cut") print(otherpart.Parent.active.Value) else print("plant not cut") print(otherpart) print(otherpart.Parent.active.Value) end end) end end
I have a model in workspace with the script, and then model's that have parts named "hit". I give the player a tool named "Tool" and it only prints "got this far" when the tool is equipped. I also put a number value into the character with the value of 1 so that the first part of the hit detection function runs. Hope that helps you in some way, the comparison is really the only thing I can see being wrong without knowing more about how your project is set up.