Hello! I have a part spawner that spawns parts very second and I want some parts to damage the player. The damage is chosen from a table of numbers and when the part is touched, the player is damaged. The problem is, it doesn’t work.
Here is my code:
local part = script.Parent
part.BrickColor = BrickColor.random()
local damageTable = {
0,
1,
10,
50,
100
}
local function onTouched(hit)
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
if not Player then return end
local Character = Player.Character or Player.CharacterAdded:Wait()
local humanoid = Player.Character:WaitForChild("Humanoid")
local leaderstats = Player:WaitForChild("leaderstats")
leaderstats.Parts.Value += 1
part:Destroy()
local damageTaken = math.random(#damageTable, 1)
humanoid:TakeDamage(damageTaken)
end
part.Touched:Connect(onTouched)
If you think you know what went wrong, please let me know. Thank you and have a great day!