Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Not opening part when players value is true?

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)

2 comments

0

Shouldn't Line09 read 'if player.leaderstats.Bucks.Value >= cost then player.leaderstats.Bucks.Value = player.leaderstats.Bucks.Value - (cost)

0

The taking away cash works

1 answer

✓ Accepted answer

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)

5 comments

0

I already have the data store

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

0

Thanks it works

0

No problem, if you wanted to help with something else contact me on discord lukiny9000_2#0558 :D.

0

I found a bug, When your value is false, It still opens

This discussion is preserved as it was. New answers, comments, and votes are closed.