Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I need help with making a clickdetector play a script, Would us help me?

Asked by 6 years ago

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.

2 answers

Log in to vote
0
Answered by 6 years ago
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
0
Hey, thanks! Chris75764 49 — 6y
0
Welp u were quicker then me. Timmerman73 85 — 6y
0
Um, now that i look this, the script haves a red line error "Expected "then" got "player", Timmer, give me your script, cause this one is broken. Chris75764 49 — 6y
0
Honestly that is quite rude of you, we're here to help, not supply you with scripts. ^ User#21242 20 — 6y
View all comments (3 more)
0
and who is "us" in "would us help me" Jo_Bot 67 — 6y
0
I see it now I changed my awnser I just copied his script and modified it a bit he forgot at line 5 to add then after if player.leaderboard.NameOfTheStat.value >= price.value changed my awnser should work now. Timmerman73 85 — 6y
0
All i did was copy his script from above and put it in a click detector function. I didn't check if his script would actually run :P justoboy13 153 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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.

0
It haves a error in the SAME. SINGLE. PART. Chris75764 49 — 6y
0
Fixed it should work now. Timmerman73 85 — 6y

Answer this question