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

Why doens´t the money value increase by one when a player clicks the part?

Asked by 5 years ago

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.

local DataStoreService = game:GetService("DataStoreService")

local MyData = DataStoreService:GetDataStore("MyData")

--Getting Data and Creating leaderboard
game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local Money = Instance.new("IntValue")
    Money.Name = "Money"
    Money.Parent = leaderstats

    local Data
    local Sucess, ErroMessage = pcall(function()
        Data = MyData:GetAsync(player.UserId.."-Money")
    end)

    if Sucess == true then
        Money.Value = Data
    else
        print("There was an error")
        warn(ErroMessage)
    end
end)

--Saving Data
game.Players.PlayerRemoving:Connect(function(player)
    local Sucess, ErrorMessage = pcall(function()
        MyData:SetAsync(player.UserId.."-Money", player.leaderstats.Money.Value)
    end)

    if Sucess == true then
        print("PlayerData sucessfully saved")
    else 
        print("There was an error")
        warn(ErrorMessage)
    end
end)

local function OnClick()
    local TrashFolder = game.Workspace.TrashFolder
    local TrashFolderChildren = TrashFolder:GetChildren()

    for i, v in pairs(TrashFolderChildren)do
        if v.Name == TrashPart then
            v.ClickDetector.MouseClick:Connect(function(player)
                player.leaderstats.Money.Value = player.leaderstats.Money.Value + 1
            end)
        end
    end
end


3 answers

Log in to vote
1
Answered by 5 years ago

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.

script.Parent.ClickDetector.MouseClick:Connect(function(player)
    player.leaderstats.Money.Value = player.leaderstats.Money.Value + 1 -- gives the player one money!
end)

If this helped you, please accept the answer!

1
please accept, as they cant debounce as it goes based off of just a click. RobloxGameingStudios 145 — 5y
1
Debounce is not needed! RobloxGameingStudios 145 — 5y
Ad
Log in to vote
1
Answered by
Farsalis 369 Moderation Voter
5 years ago

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:

local Debounce = false

workspace.Part.ClickDetector.MouseClick:Connect(function(plr)
    if Debounce == false then
        Debounce = true
        plr.leaderstats.Money.Value = plr.leaderstats.Money.Value + 1
    end
    wait(2) -- So They Cant Spam The Click Detector
    Debounce = true
end)

Hope This Helped!

1
debounce isnt needed, they cant spam by default. RobloxGameingStudios 145 — 5y
1
I don't know what you consider spam, but yeah they can definitely, indiscriminately click the part. just a simple debounce, so that they can't click it continously, Farsalis 369 — 5y
Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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.

local y = game.Players.LocalPlayer

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.--

local cash = 1 -- The amount of cash u wanna give--

script.Parent = workspace -- The script's parent can't be the part because it will cause an error in this script.--

local p = workspace:FindFirstChild("BlaBlaBla")
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--

r.MouseButton1Click:Connect(function(mouseClick, recievedData)
if mouseClick and recievedData then return end
       y.leaderstats.Cash.Value = y.leaderstats.Cash.Value + cash
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

0
Alright I answered this a year ago and sheeeshh I was dumb back then. Why tf am I using letters for variables, I use keywords nowadays for variables. TheQuitzzyKadren 29 — 3y

Answer this question