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

Why This Script is not killing players when they touch the Random Chosed Map?

local mapStorage = game.ServerStorage.Maps:GetChildren()

local chosenMap = mapStorage[math.random(1, #mapStorage)]
chosenMap.Parent = workspace.CurrentPlaying
print("Map chosen is; "..chosenMap.Name.." Map")

local touchMapDie = chosenMap:GetChildren()
	
for i, v in pairs(game.Players:GetPlayers()) do
			touchMapDie.Touched:Connect(function(hit)
		hit.Parent.Humanoid.Health = 0
		end)
end

So Basically, a random map generates, then when the players touch that map (every single part of the map), They are supposed to die

3 comments

1

At line 10 put if v.touchmapDie.Touched:Connect(function(hit) then

0

Still, it doesn't work @JesseSong

1

Check my answer :D

1 answer

✓ Accepted answer

You can't check if children were touched using :GetChildren(), the correct way to do this is using a for loop and get the children from there.

local mapStorage = game.ServerStorage.Maps:GetChildren()
local Players = game:GetService("Players")
local chosenMap = mapStorage[math.random(1, #mapStorage)]
chosenMap.Parent = workspace.CurrentPlaying
print("Map chosen is; "..chosenMap.Name.." Map")

local touchMapDie = chosenMap:GetChildren()

for i, v in pairs(touchMapDie) do --Gets the children of the chosenMap
	if v:IsA("Part") then
	v.Touched:Connect(function(hit) --Check if a child was touched
		if Players:GetPlayerFromCharacter(hit.Parent) then --Check if the part touching is a player
			hit.Parent.Humanoid.Health = 0 --Kills the player
		end
	end)
	end
end

4 comments

0

Touched is not a valid member of Model, I Tried to do v.Touched.Parent:Connect*(function(hit) but still it didn't work

0

Ok, I updated it, this should work.

0

Now I Get This Error: Unable to cast value to Object

0

Which line?

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