When a player hits an enemy, the script places a boolvalue with the player who hit the enemies name. How would i make it to where it detects that the boolvalue’s name IS a players name?
Make a stringvalue called Owner then add a player name, then a boolvalue which indicates if it has an owner or not.
how would i make the boolvalue check if its a player? sorry if im asking a dumb question
No use ObjectValue that references the character instance, instead of its name.
Thanks for the feedback but how would i make it to where it can detect that the objectvalue’s value and the players name are the same?
local name = objectValue.Value.Name
if name == player.Name then
-- proceed
end
my script is a serverscript and its a reward script so the player isnt defined, thats why i need the player to be defined from the object value.
If you wish to acquire the player instance from the value then:
local player = objectValue.Value
To set the player as the value then:
objectValue.Value = player
alright thank you ill try it now
You cannot use a BoolValue for that, as it only stores a true/false, thus the name Bool. You can use a StringValue (string variables store a mix of letters and numbers used to store information, i.e names.)
What are you trying to do? Make a weapon and detect when a player hits an enemy? I’m not sure I understand as you are attempting a different approach here.
String value will fork instances with similar names, object values will not as it references the instance directly that do not need further algorithms to index from the name.
I have skills in my game and when the fx hits the enemy and kills them, you dont get a reward. Thats what im trying to fix i want skills to be able to give rewards.
This is what the reward script looks like
local Enemy = script.Parent.Humanoid
function KillForXp()
local tag = Enemy:findFirstChild("creator")
if tag ~= nil then
if tag.Value ~= nil then
local Data = tag.Value:findFirstChild("leaderstats")
local Data0 = tag.Value:findFirstChild("Leaderstats")
if Data ~= nil then
if Data~= nil then
Data.Gold.Value = Data.Gold.Value + 10
Data0.XP.Value = Data0.XP.Value + 80
wait(0.5)
script:Destroy()
end
end
end
end
end
Enemy.Died:connect(KillForXp)
The method I use to detect when a player has killed an enemy is via StringValues and adding a tag to the enemy player whenever the player attacked them. In your damage script, add this code in:
local Tagged = Instance.new(“StringValue”)
Tagged.Name = “Tagged”
Tagged.Value = character.Name — The player who attacked the enemy
Tagged.Parent = EnemyCharacter
game:GetService(“Debris”):AddItem(Tagged, .5) — 0.5 Seconds always works for me, don’t change this to a larger integer
Create a Script in ServerScriptService, and write this down:
local amount = 5 — Change to your amount of rewards to give
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
character:FindFirstChild(“Humanoid”).Died:Connect(function()
if character:FindFirstChild(“Tagged”) then
local winner = game.Players:FindFirstChild(character:FindFirstChild(“Tagged”).Value)
if winner then
winner.Reward.Value += amount
end
end
end)
end)
end)
The issue is that when the effects from the skill hit the enemy and the enemy dies there is no reward there is no reward problems with hitting the enemy with the sword.
Thanks ill try the code right now.
No problem, if you’re attacking an NPC you might have to write the code differently, keep that code though if you want to gain rewards when attacking players.
Oh this whole code is for pve not pvp, sorry
That’s fine, keep the tagging code above and remove the script from ServerScriptService. In the NPC Died event code you sent earlier, remove all the contents in the function and replace it with this:
— Put these lines of code above the function
local amount = 5 — Put this
local Character = script.Parent
— Put these lines of code inside the function
if not Character.Tagged then return end
local Tagged = Character.Tagged
local player = game.Players:FindFirstChild(Tagged.Value)
if not player then return end
player.Character.Rewards.Value = amount — Change Rewards to the currency you want to give to the player as a reward