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

If stats script isnt working despite no errors?(SOLVED)

This is a local script inside a part. What is it supposed to do is if this player has that many stats, it is transparent. however it dosent work and it should be. any help please?

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Coins = LocalPlayer:WaitForChild("leaderstats").Coins
if Coins.Value == 2 then
	script.Parent.Transparency = 1
end

4 comments

0

is there any infinite yield in the output? and is the code in a localscript?

0

You can't use a localscript inside a part. Instead- move the script to ScreenGui.

0

yes, but i want it to effect a a part, not a gui. :/

0

also i tried making it effect the brick through game.workspace, still didnt work in a gui.

3 answers

✓ Accepted answer

Make sure this is a LocalScript and that it is located inside StarterGui since LocalScripts must be parent to something in connection to the player to work.

From here we will set a variable to get to the part and implement a changed even to check when the coin value changes.

local plr=game.Players.LocalPlayer
local coin=plr:WaitForChild("leaderstats").Coins
local part=workspace.Part --change to the part you want to make in/visible

function change()
	if coin.Value>=2 then
		part.Transparency=1
	else
		part.Transparency=0
	end
end
coin.Changed:Connect(change)
change()

I changed the if statement to >= so if you also have more than 2 coins it will be transparent. I also made it so that if you have less than 2 it will show.

U have to check if the value changes before the if statement otherwise the script runs basically reading

if coins ==2 then, coins doesn't equal 2 so ends the script.<

should look like this,

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Coins = LocalPlayer:WaitForChild("leaderstats").Coins
Coins.Changed:Connect(function()
	if Coins.Value == 2 then
   	 script.Parent.Transparency = 1
	end
end)

2 comments

0

plz accept answer

0

it didnt work. coin value is 2, thing is not transprent.

PLEASE READ THIS BEFORE COPYING CODE!

If you hover over a local script when inserting something into an object it'll read that it can only run under certain conditions, such as if it's inside of a character model, inside of a GUI, inside of the ReplicatedFirst service, etc. It didn't work since it didn't fulfill any of the conditions above, therefore not running.

So how do we fix this? Here's how;

(I'm assuming you want local parts. If you don't want that then use RemoteEvents.)

local Player = game:GetService("Players").LocalPlayer
local Part = workspace.Part

local Coins = Player.leaderstats.Coins

function onChanged()
	if Coins.Value >= 2 then
		Part.Transparency = 1
	end
end

Coins:GetPropertyChangedSignal("Value"):Connect(onChanged)

In the code above we define the player, part, and coins in separate variables. We also have a function that fires every time value of Coins changes. we check if the value is 2 and if that's the case we change the part's transparency property to 1. The code above should be placed as a local script inside of the StarterPlayerScripts.

We do this on purpose so the part's transparency doesn't replicate to other players.

Edit: Another problem is that the conditional statement only runs once not to mention the conditional also doesn't run if the value of "Coins" the greater than 2. Fixed!

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