Hello!
I’m attempting to make a fighting game. I use remote events every time a kill is made, and a player’s textlabel will update every time they get a kill. The thing is, it’s not working. The error was something like: “Player argument must be a player object”. I tried fixing it, but it keeps saying the error. Any help is appreciated:
ServerScriptService Script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local KillEvent = ReplicatedStorage:WaitForChild("KillEvent")
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:FindFirstChild("Humanoid")
humanoid.Died:Connect(function()
local tag = humanoid:FindFirstChild("creator")
local killer = tag.Value
if tag and killer then
KillEvent:FireClient(tag.Parent.Parent, kill)
end
end)
end)
end)
Kill counter gui localscript:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local KillEvent = ReplicatedStorage:WaitForChild("KillEvent")
KillEvent.OnClientEvent:Connect(function(killer, kill)
script.Parent.Text = "Kills:"..kill
end)
Can you run it again and see what line the error says its from
(Click the error in the output and it will bring you to that line)
Where did the kill variable exactly come from?
I located your error should be within the definition of the player’s killer
basically, change it to
KillEvent:FireClient(tag.Value, kill)
Make sure to only add in the kill if it’s a valid variable (e.g if it holds the player’s kill amount)
Try doing this:
local Player = game.Players:GetPlayerFromCharacter(tag.Parent.Parent)
KillEvent:FireClient(Player, kill)
Well I got the player figured out, I just need to know how to change the gui when the player gets a kill. (I’m using swords for the game)
Change your OnClientEvent to this
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local KillEvent = ReplicatedStorage:WaitForChild("KillEvent")
local plr = game:GetService("Players").LocalPlayer
KillEvent.OnClientEvent:Connect(function(kill)
script.Parent.Text = "Kills:"..kill
end)
Unlike OnServerEvent, the first parameter cannot be the player who fired the event, as OnClientEvent is in a localscript, hence, you already the got player via LocalPlayer
, you still have to give it the player to fire to as tthe first thing in FireClient
. I think this should work
Edit: Looking closely, you don’t have anything set up to kill
in the server, so you’d have to store the amount of kills a player has somewhere and update that
And how would I go about that? I’m learning how to script, so I don’t really know much.
You could put an IntValue in the player when they join and when someone dies, get the killer’s intvalue and return that with 1 added to it. SOmething like this
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local KillEvent = ReplicatedStorage:WaitForChild("KillEvent")
game.Players.PlayerAdded:Connect(function(player)
--Code to insert the IntValue
local kills = Instance.new("IntValue")
kills.Value = 0
kills.Name = "Kills"
kills.Parent = player
player.CharacterAdded:Connect(function(character)
local humanoid = character:FindFirstChild("Humanoid")
humanoid.Died:Connect(function()
local tag = humanoid:FindFirstChild("creator")
local killer = tag.Value
local killerKills = killer.Kills --Gets the Kills IntValue from the killer
if tag and killer then
killerKills.Value += 1
KillEvent:FireClient(killer, killerKills.Value)
end
end)
end)
end)
Edit: Forgot to make the killer’s kills also increase by one haha
1 Like