I have recently been making a game, where I need the money value to rise by one each time the player clicks a part however, it doesn´t work and I can figure it out. Help would be appreciated and I will accept a good answer.
01 | local DataStoreService = game:GetService( "DataStoreService" ) |
02 |
03 | local MyData = DataStoreService:GetDataStore( "MyData" ) |
04 |
05 | --Getting Data and Creating leaderboard |
06 | game.Players.PlayerAdded:Connect( function (player) |
07 | local leaderstats = Instance.new( "Folder" ) |
08 | leaderstats.Name = "leaderstats" |
09 | leaderstats.Parent = player |
10 |
11 | local Money = Instance.new( "IntValue" ) |
12 | Money.Name = "Money" |
13 | Money.Parent = leaderstats |
14 |
15 | local Data |
This is a lot of code that I have come to find unnecessary. I can see your also trying to make a data store in the same thing, Here's one word, Don't. Here's a better script:
This is a normal script inside a part with a click detector. All Code Tested.
1 | script.Parent.ClickDetector.MouseClick:Connect( function (player) |
2 | player.leaderstats.Money.Value = player.leaderstats.Money.Value + 1 -- gives the player one money! |
3 | end ) |
If this helped you, please accept the answer!
Hey Andrius, first off. I wouldn't really try, to access a part through a Data Script. Since it's kinda it's own thing. Also, It might be, because when you set the money and where you actually add the money aren't on the same line. Or, when you access TrashPart(47) its as an object, not a string. If your just using the for loop, to find TrashPart directly. Just access it through workspace.TrashFolder directly. Add a WaitForChild() if you need to. This Code Should Work:
01 | local Debounce = false |
02 |
03 | workspace.Part.ClickDetector.MouseClick:Connect( function (plr) |
04 | if Debounce = = false then |
05 | Debounce = true |
06 | plr.leaderstats.Money.Value = plr.leaderstats.Money.Value + 1 |
07 | end |
08 | wait( 2 ) -- So They Cant Spam The Click Detector |
09 | Debounce = true |
10 | end ) |
Hope This Helped!
So basically you're using a server script service script, you have to put the touched script in the workspace or make a new script in the workspace and cut the touched function but instead of what you put you have got to do this.
01 | local y = game.Players.LocalPlayer |
02 |
03 | local x = game.Players:GetChildren() -- This isn't necessary if you just want that player to get it but I'm just going to include it.-- |
04 |
05 | local cash = 1 -- The amount of cash u wanna give-- |
06 |
07 | script.Parent = workspace -- The script's parent can't be the part because it will cause an error in this script.-- |
08 |
09 | local p = workspace:FindFirstChild( "BlaBlaBla" ) |
10 | local r = p:FindFirstChildOfClass() -- This is so it looks for the one thing in the part which is going to be click detector. P.S you have to put this script in workspace-- |
11 |
12 | r.MouseButton 1 Click:Connect( function (mouseClick, recievedData) |
13 | if mouseClick and recievedData then return end |
14 | y.leaderstats.Cash.Value = y.leaderstats.Cash.Value + cash |
15 | end |
This is just one of the many ways this can be fixed, this is just an example so I really have no clue if it will work but I hope it does and if it does, no need to thank me. I will always be here to help! --Kadren aka/ TheQuitzzyKadren