How would I go about making a "Ability Shop" [Solved]

I am after someone who knows how to basically have a script, not a local script inside a button, that upon click if. the player who clicks, has say 100 kills then they can equip the ability “kinetic”

this is what I got, doesn’t work tho, as always

local plrs = game:GetService("Players")

script.Parent.MouseButton1Click:Connect(function(plr)
	local player = plrs:GetPlayerFromCharacter(plr)
	if player.leaderstats.Kills.Values >= 50 then
		player.leaderstats.Ability.Value = "Kinetic"
	end
    
	
end)

Btw it doesn’t work because this error is what appears:
“Attempt to index nil with leaderstats” on line 5

[SOLVED]
thanks to:
xE_nte
EveningNighmare

2 Likes

That basicly means it can’t find the leaderstats folder of the player. (I guess you create it in another script) But the real problem ist

You already get the player (plr) in the MouseButton1Click Funktion. So most likely player will be nil in your script. Try replacing player.leaderstats with plr.leaderstats

1 Like

thanks I will try that, I appreciate this

1 Like

Update, tried that and still same issue

code now:



script.Parent.MouseButton1Click:Connect(function(plr)
	if plr.leaderstats.Kills.Values >= 50 then
		plr.leaderstats.Ability.Value = "Kinetic"
	end
    
	
end)

try debugging the code with adding print(plr) or print(plr.Name), also do you create the leaderstats in another script?

yep, here’s leader stats script (in serverscriptsevice)

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = plr
	leaderstats.Name = "leaderstats"
	
	local tempkills = Instance.new("IntValue")
	tempkills.Parent = leaderstats
	tempkills.Name = "KillsRound"
	tempkills.Value = 0
	
	local totalkills = Instance.new("IntValue")
	totalkills.Parent = leaderstats
	totalkills.Name = "Kills"
	totalkills.Value = 0
	
	local roundsplayed = Instance.new("IntValue")
	roundsplayed.Parent = leaderstats
	roundsplayed.Name = "Rounds"
	roundsplayed.Value = 0
	
	local ability = Instance.new("StringValue")
	ability.Parent = leaderstats
	ability.Name = "Ability"
	ability.Value = "Default"
end)

MouseButton1Cliick event doesn’t return Player as an argument. You have to use LocalPlayer (and dont change leaderstats on the client side lol)

1 Like

this script is not local script

Find player in a different way, you cannot get player from Mousebtn1click event

woops forgot about that :skull:
You could the get the player by doing local player=script.ParentParent.Parent…
(depends on where the button is located)

ok well ill give this a try. let ya know if it works

just use script:FindFirstAncestorOfClass(“Player”), it is a lot of shorter and better optimized

2 Likes

this works so now the code is:

script.Parent.MouseButton1Click:Connect(function(plr)
	local player = script:FindFirstAncestorOfClass("Player")
	if player.leaderstats.Kills.Value >= 50 then
		player.leaderstats.Ability.Value = "Diamond"
	end
    
	
end)

Posting this for others to find

1 Like

if someones confused how it works: its because this script is descendant of player

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.