Hello! I’ve tried to script a part which automatically kills anyone who is on the team “NEMO” (it also rotates thanks to HingeConstraints). However, I get this error.
I’m not really sure what’s wrong, because the player variable should be correct therefore there should be a Team property which can be checked.
The script:
local SoundService = game:GetService("SoundService")
local FailSound = SoundService:WaitForChild("CourseFail")
for i,v in pairs(workspace:GetChildren()) do
if v.Name == "KillBlock" then
KillBlock = v
end
end
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage.FailGUI
KillBlock.Touched:Connect(function(hit)
local Humanoid = hit.Parent:FindFirstChild("Humanoid")
local player = game:GetService("Players"):FindFirstChild(hit.Parent.Name)
if Humanoid then
if player.Team == game.Teams.NEMO then
Humanoid.Health = 0
FailSound:Play()
local Highlight = Instance.new("Highlight")
Highlight.Parent = hit.Parent
Event:FireAllClients(hit)
end
end
end)
Does any sort of error occur once it happens, or does nothing at all happen?
You could try this piece of code I wrote, but I am not sure if it’s going to work…
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SoundService = game:GetService("SoundService")
local Teams = game:GetService("Teams")
local event = ReplicatedStorage.FailGUI
local failSound = SoundService:WaitForChild("CourseFail")
local killBlock
for _, child in workspace:GetChildren() do
if child.Name ~= "KillBlock" then
continue
end
killBlock = child
continue
end
local function onTouched(hitPart: BasePart)
local hitCharacter = hitPart:FindFirstAncestorOfClass("Model")
if not hitCharacter then
return
end
local hitHumanoid = hitCharacter:FindFirstChildOfClass("Humanoid")
if not hitHumanoid or hitHumanoid.Health <= 0 then
return
end
local playerHit = Players:GetPlayerFromCharacter(hitCharacter)
if not playerHit or playerHit.Team ~= Teams.NEMO then
return
end
hitHumanoid.Health = 0
failSound:Play()
local highlight = Instance.new("Highlight")
highlight.Parent = hitCharacter
event:FireAllClients(hitPart)
end
killBlock.Touched:Connect(onTouched)
I tested your script and it seems to work, I’m guessing the player somehow returned as nil? Try using the function :GetPlayerFromCharacter() instead of finding the player in the players properties?
Is there only one instance in workspace named KillBlock? If so, you could replace the loop with workspace:FindFirstChild("KillBlock"). Otherwise? It may be getting the wrong instance.
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SoundService = game:GetService("SoundService")
local Teams = game:GetService("Teams")
local event = ReplicatedStorage.FailGUI
local failSound = SoundService:WaitForChild("CourseFail")
local function onTouched(hitPart: BasePart)
local hitCharacter = hitPart:FindFirstAncestorOfClass("Model")
if not hitCharacter then
return
end
local hitHumanoid = hitCharacter:FindFirstChildOfClass("Humanoid")
if not hitHumanoid or hitHumanoid.Health <= 0 then
return
end
local playerHit = Players:GetPlayerFromCharacter(hitCharacter)
if not playerHit or playerHit.Team ~= Teams.NEMO then
return
end
hitHumanoid.Health = 0
failSound:Play()
local highlight = Instance.new("Highlight")
highlight.Parent = hitCharacter
event:FireAllClients(hitPart)
end
for _, child in workspace:GetChildren() do
if child.Name ~= "KillBlock" then
continue
end
child.Touched:Connect(onTouched)
end