Hola Robloxians,
I have a script in my game that whenever a player is killed you get 5 points, and I have made a special zone so if you kill the player in that place, you should get 5+3 points (which means 5 points for normal script and 3 if you are in that area.)
How can I make something like that?
Here is my script:
game.Workspace.Areas.Main2.Touched:Connect(function)
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(Character)
Character.Humanoid.Died:Connect(function(Died)
local creator = Character.Humanoid:FindFirstChild("creator")
local leaderstats = creator.Value:FindFirstChild("leaderstats")
if creator ~= nil and creator.Value ~= nil then
leaderstats.Coins.Value = leaderstats.Coins.Value + 3
end
end)
end)
end)
end)
You can have an invisible part where your special zone is. When a kill happens, you raycast under the player and see if it hits the invisible part. If it does then give another extra 3 points.
You can do this for more special areas as well. You can have all the invisible parts in a model.
part:GetTouchingParts() can be used to tell what objects are touching a part. The part must have a TouchInterest for this to work. A TouchInterest is created when you connect to part.Touched.
local D = workspace:GetPartsInPart(Character.HumanoidRootPart, OverlapParams.new({}))
for _,v in D do
if tostring(v):lower():match("zone") then
leaderstats.Coins.Value += 8
end
end
replace zone with a lower case version of the part name.
You definitely shouldn’t create a new PlayerAdded connection every time the part is Touched.
Instead, your code should check if the player is inside the zone inside the if statement.
For example, you could use a for loop over the parts returned by game.Workspace.Areas.Main2:GetTouchingParts() and check if any of the parts are the player who ko’ed a player. If they are, then add the 3 coins.