First of all, im really newbie with scripting, but im trying to learn, heck, i have used the studio more than i have been playing roblox for the last like... 5 weeks?
So, i want to make a script that changes the leaderstat of a player, i know how to subtract or/and add a leaderstat value after purchasing stuff by doing something like this:
--Note, this is just a recreation of the script, stuff may not be functional in roblox studio. player = LocalPlayer leaderboard = Waitforchild("leaderstats") price = 10 if player.leaderboard.NameOfTheStat.value >= price.value --checks if the Leaderboard stat is the same or higher than "price" player.leaderboard.NameOfTheStat.value = player.leaderboard.NameOfTheStat.value - price.value --Changes "nameofthestat" to be subtracted - 5 units. player.leaderboard.AnotherNoNameStat.value = player.leaderboard.AnotherNoNameStat.value + 1 --Changes "anothernonamestat" to be added one unit. --btw thanks roblox wiki
So, i wanna give use of that script inside a part with a clickdetector, but that when the part gets clicked it makes that script.
How can i make it?, and can it work with filtering enabled?, Because my game haves filtering enabled.
function onMouseClick(player)--this is the function and player is the player who clicked local leaderboard = Waitforchild("leaderstats") local price = 10 if player.leaderboard.NameOfTheStat.value >= price.value --checks if the Leaderboard stat is the same or higher than "price" player.leaderboard.NameOfTheStat.Value = player.leaderboard.NameOfTheStat.Value - price --Changes "nameofthestat" to be subtracted - 10 units. player.leaderboard.AnotherNoNameStat.Value = player.leaderboard.AnotherNoNameStat.Value + 1 --Changes "anothernonamestat" to be added one unit. end clickDetector.MouseClick:connect(onMouseClick)--when a player clicks, run function onMouseClick
To first start, you could better run this from a script instead of a localscript ( assuming this because you're using localplayer) Then just insert a clickdetector and look at its event so you would have something like this
local clickdetector = Instance.New('ClickDetector') clickdetector.Parent = script.Parent --The event that fires when the clickdetector is clicked clickdetector.MouseClick:Connect(function() --Note, this is just a recreation of the script, stuff may not be functional in roblox studio. player = LocalPlayer leaderboard = plr:Waitforchild("leaderstats") price = 10 if leaderboard.NameOfTheStat.value >= price.value then --checks if the Leaderboard stat is the same or higher than "price" leaderboard.NameOfTheStat.value = player.leaderboard.NameOfTheStat.value - price.value --Changes "nameofthestat" to be subtracted - 5 units. leaderboard.AnotherNoNameStat.value = leaderboard.AnotherNoNameStat.value + 1 --Changes "anothernonamestat" to be added one unit. --btw thanks Roblox wiki end)
If you want to make it FE compatible and safer use something like this in a script.
local clickdetector = Instance.New('ClickDetector') clickdetector.Parent = script.Parent --The event that fires when the clickdetector is clicked clickdetector.MouseClick:Connect(function(plr) --Note, this is just a recreation of the script, stuff may not be functional in roblox studio. player = plr leaderboard = plr:Waitforchild("leaderstats") price = 10 if leaderboard.NameOfTheStat.value >= price.value then --checks if the Leaderboard stat is the same or higher than "price" leaderboard.NameOfTheStat.value = leaderboard.NameOfTheStat.value - price.value --Changes "nameofthestat" to be subtracted - 5 units. leaderboard.AnotherNoNameStat.value = leaderboard.AnotherNoNameStat.value + 1 --Changes "anothernonamestat" to be added one unit. end end)
Here you are passing a Parameter in the function which is telling your script who the player is. so the server can handle this instead of the client
Consider accepting this answer if it helped you as it will give us both reputation.