Part not giving wins in leaderstats

Hello!

When trying to write a script for when a player touches a part it kills them and rewards with 1 win. I have gotten the kill part to work but not to win. The script is a server script. I have got this error too:
19:23:10.736 Workspace.FlagQuiz.Done.Part.KillScript:8: attempt to perform arithmetic (add) on Instance and number - Server - KillScript:8

Script:

local Kill = script.Parent
game.Players.PlayerAdded:Connect(function(plr)
	local wins = plr.leaderstats.Wins
	
	Kill.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent.Humanoid.Health = 0
		wins = wins + 1
	end
end)
	
end)
1 Like
wins.Value += 1

That’s the fix, but I recomend also moving this event outside of the playeradded event, this would probably be more beneficial on resources and also look cleaner, not sure why you put it in a playeradded event.

2 Likes

What you were doing originally was adding an instance, since you weren’t accessing the value property:

That’s the reason for your error.

2 Likes

Thank you! I have just added a wait() so it dosent add alot of wins :slight_smile:

2 Likes

Oh nice, also “wait()” is old and I recommend using task.wait() instead as it’s the new version and has more benefits.

1 Like

And again, I recommend moving that .Touched event outside of the playeradded event. This current code would lead to memory leaks which would overall impact the player’s experience.

Side question, are you new to coding?

1 Like

Yes i am learning coding but i have a problem with the script. When I have reloaded and aren’t touching the part it still says that I am getting an extra 24 - 30 wins when i die instead of 1

Script:

local Kill = script.Parent
game.Players.PlayerAdded:Connect(function(plr)
	local wins = plr:WaitForChild("leaderstats").Wins
	
	Kill.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent.Humanoid.Health = 0
		wait(3)
		wins.Value = wins.Value + 1
		wait(10)
	end
end)
	
end)
2 Likes

I recommend you learn about debouncing, debouncing are boolean variables that are used for cooldowns like this. When you want an event to run a certain amount of times or what not.

1 Like

I will learn that after, because this is urgent but do you know how i could alter the script to make it work please?

2 Likes

I think you should use a Remote Event, that when a part is touched get the local player, and fire the event that assign the value to win

2 Likes

Yeah sure, give mea second to write out some code.

2 Likes

I guess that is another thing to learn after this. Will do :slight_smile:

1 Like

Change this to a local script and store it in starterplayerscripts. And add a remote event into replicatedstorage.

-- Local script
local RepStorage = game:GetService("ReplicatedStorage")
local RE = Repstorage:WaitForChild("RemoteEvent")
local Kill = workspace:WaitForChild("Kill")
local db = false

Kill.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if db == false then
db = true
RE:FireServer(hit)
task.wait(3)
RE:FireServer()
task.wait(10)
db = false
end
RE.OnServerEvent:Connect(function(player, hit)
if hit then
hit.Parent.Humanoid.Health = 0
else
local wins = player.leaderstats.Wins
wins.Value += 1
end

I’m in a bit of a hurry so I can’t write the best most secure code but for now this would be good

1 Like

Sorry about to edit the code because it might not work due to stuff not loading in on time. This is why waitforchild is so important on the client.

1 Like

But if you’re still learning how to use events, functions, printing, variables, etc. I recommend going back a bit before attempting harder stuff like this. These are more advanced fundamentals that you should know after learning the basics of luau.

1 Like

I got this error when testing the script
20:11:23.539 OnServerEvent can only be used on the server - Client - LocalScript:18

1 Like

You’re meant to make a seperate script in serverscriptservice for that part. Sorry for not specifying.

Also I realised this code is easily exploitable by hackers but i’m in a hurry as long as it works for now.

1 Like

Also copy and paste the code again because I just edited it. I’ll give you some better code for this later on, using attributes from the server and what not. (I’ll explain that after).

Now i dont die anymore i just sit there :frowning:

1 Like

I have added a kill script and when I die it still dosen’t give me 1 win

1 Like