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

Not opening part when players value is true?

Asked by
MattVSNNL 620 Moderation Voter
2 years ago

I'm making an area inside of my game, I made when you touch a part and you have enough bucks it opens and then a value goes true making sure that they own it, So the value saves, But I wrote a code to check if they have that value true, It opens, But it doesn't work, Can anyone help?

Here's my code:

local cost = 100

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then

        local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)

        if player.leaderstats.Bucks.Value >= cost then
            player.leaderstats.Bucks.Value -= cost

            player:WaitForChild("Areas"):WaitForChild("HasForest").Value = true

            script.Parent.Transparency = 1
            script.Parent.CanCollide = false

            script.Disabled = true
        end
    end
end)

game:GetService("Players").PlayerAdded:Connect(function(player)
    if player:WaitForChild("Areas"):WaitForChild("HasForest").Value == true then
        script.Parent.Transparency = 1
        script.Parent.CanCollide = false
    end
end)
0
Shouldn't Line09 read 'if player.leaderstats.Bucks.Value >= cost then player.leaderstats.Bucks.Value = player.leaderstats.Bucks.Value - (cost) ABK2017 406 — 2y
0
The taking away cash works MattVSNNL 620 — 2y

1 answer

Log in to vote
1
Answered by 2 years ago

Hello, I hope this will help:

local cost = 100
local dataStore = game:GetService("DataStoreService")
local data = dataStore:GetDataStore("SavedData")
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then

        local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)

        if player.leaderstats.Bucks.Value >= cost then
            player.leaderstats.Bucks.Value = player.leaderstats.Bucks.Value - cost -- I know this works in python, but I changed that to be sure it will work

            player:WaitForChild("Areas"):WaitForChild("HasForest").Value = true

            script.Parent.Transparency = 1
            script.Parent.CanCollide = false

            local dataToSave = {
                ownsForest = "true"
            }

            data:SetAsync(player.UserId, dataToSave)
            wait(0.5)

            script.Disabled = true
        end
    end
end)

game:GetService("Players").PlayerAdded:Connect(function(player) -- Dudeee, if you didnt set data store to save the value it wont work, because when you join game it is false by default
    local hasSavedData = data:GetAsync(player.UserId)
    if hasSavedData then
        if hasSavedData.ownsForest == "true" then
            script.Parent.Transparency = 1
            script.Parent.CanCollide = false
        end
    end
end)
0
I already have the data store MattVSNNL 620 — 2y
0
Replace it with my script. When both script runs (one for datastore, second for this), this one will probably run faster than datastore one, so before it changes value to true if player already owns it, that script saw its false because its faster BeautifulAuraLover 371 — 2y
0
Thanks it works MattVSNNL 620 — 2y
0
No problem, if you wanted to help with something else contact me on discord lukiny9000_2#0558 :D. BeautifulAuraLover 371 — 2y
0
I found a bug, When your value is false, It still opens MattVSNNL 620 — 2y
Ad

Answer this question