I have a script that gives xp when the player kills a monster. Whoever gets the last hit gets the xp. I want to change the script to give the xp to the entire team. There are multiple teams in the game.
Here is how the script works in detail.
Step 1. The monster has a script that calls for a function.
local hum = script.Parent:WaitForChild("Humanoid")
hum.Died:Connect(function()
game.ServerStorage.GiveXP:Fire(hum.creator.Value)
end)
Step 2
This calls for the Bindable Event called GiveXP which is in Server Storage.
Step 3
I have a leaderboard script inside ServerScriptService that handles the player actually getting more experience.
game.ServerStorage.GiveXP.Event:Connect(function(plr)
if player.Name == plr.Name then
experience.Value += giveExpAmount
end
end)
So I have some ideas about changing the servicescriptservice script to handle it giving the xp to anyone who is on the same team as the player who killed the monster but it is still confusing me. Anyone have any ideas on how to accomplish this?
The way you would go about it is to check if the player is in a team, if they are, grab that team and use Team:GetPlayers()
to get all the players on that team as a table, loop through the table and award each player
Something like this for example
game.ServerStorage.GiveXP.Event:Connect(function(plr)
if not plr.Team then
-- Here you can either return to stop here
-- Or only give the exp to the player who killed the enemy
end
local teamPlayers = plr.Team:GetPlayers()
for _, teamPlayer in ipairs(teamPlayers) do
-- Grab the experience value
experience.Value += giveExpAmount
end
end)
Im going to test this.
local Teams = game:GetService("Teams")
local giveExpAmount = 8
game.ServerStorage.GiveXP.Event:Connect(function(plr)
if not plr.Team then
return
end
local teamPlayers = plr.Team:GetPlayers()
for _, teamPlayer in ipairs(teamPlayers) do
experience.Value += giveExpAmount
end
end)
it doesnt like
if not plr.Team then
It says player is "team is not a valid member of model “Workspace.Darkwulf”
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")
local giveExpAmount = 8
game.ServerStorage.GiveXP.Event:Connect(function(char)
local plr = Players:GetPlayerFromCharacter(char)
if not plr.Team then
return
end
local teamPlayers = plr.Team:GetPlayers()
for _, teamPlayer in ipairs(teamPlayers) do
experience.Value += giveExpAmount
end
end)
Try that.
sorry it took me awhile to respond. It took a lot of testing.
This gives experience to everyone, even if they are on the wrong team. Also anyone using a ranged weapon (bow or crossbow) doesn’t get experience because the projectile is not part of the team, I assume.