Roblox image transparency does not change - lua

I am trying to make a system where if the player has a specific item in their file(a file added to the player) an Gui image label background transparency is changed to tell the user if he has clamed the item or not. I tried it and nothing happened, was the same transparency as I left it.
here is my code:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local catscollected = player:WaitForChild("catsCollected")
if catscollected:FindFirstChild("sleeping cat") then
script.Parent.Transparency = 1
else
script.Parent.Transparency = 0.25
end

Your code is only running once, and that is when the player joins. If you want it to keep checking, put it in a loop:
while true do
if catscollected:FindFirstChild("sleeping cat") then
script.Parent.Transparency = 1
else
script.Parent.Transparency = 0.25
end
wait()
end
Note that this loop will hold the script, so anything after the loop won’t run. To fix that, I recommend you put this in its own script (if it isn’t already).

Related

The door data is not saved in Roblox

I wrote a door save system.
That is, if the user previously bought them, then when re-entering the game, they must be open.
My code works, but the door doesn't save at all.
-- DoorsDataStore
-- Save Stats Doors
local opend = false
local datastorage = game:GetService("DataStoreService")
local isitopen_1 = datastorage:GetDataStore("Door")
game.Players.PlayerAdded:Connect(function(player)
local boolValueDoors = Instance.new("Folder")
boolValueDoors.Name = "BoolValueDoors"
boolValueDoors.Parent = player
local door_1 = Instance.new("BoolValue")
door_1.Parent = boolValueDoors
door_1.Name = "BoolValueDoor_1"
door_1.Value = isitopen_1:GetAsync(player.UserId)
print("True or False")
print(player.BoolValueDoor_1.Value)
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, erromsg = pcall(function()
isitopen_1:SetAsync(player.UserId, player.BoolValueDoor_1.Value)
end)
if erromsg then
warn("Error")
end
end)
-- TouchDoor
script.Parent.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if (humanoid ~= nil) then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player.leaderstats.Coins.Value >= script.Parent.Price.Value then
player.leaderstats.Coins.Value -= script.Parent.Price.Value
player.leaderstats.Level.Value += 1
script.Parent:Destroy()
player.BoolValueDoors.BoolValueDoor_1.Value = true
print("Save Door")
end
end
end)
I tried writing this code in different ways, in different versions, tried through validation. My code still doesn't do what I want.
There are multiple possible problems I see:
1.
SetAsync takes a string as the first argument, you are giving it a number value. To fix this use tostring(player.UserId)
2.
When the player first joins, the value will be set to nil, because there is no data in the datastore under the UserId, and nil is not a boolean.
I’m not sure if these are actual issues and I currently cannot check if these are real problems because I’m not on my computer but they may be something you want to change.
Also it would be nice to know if you encountered any errors when executing the code.
You should also make trigger a remote event that opens the door on the clientside in the PlayerAdded function if the value is true.

attempt to index nil with leaderstats

I'm making a game where you ram cars into houses and it unanchored the parts that are touched by a part in the front of the car. I want to have it so that whenever you unanchor a part, you get a coin
local HitPart = script.Parent
local function onTouch(otherPart)
local player = otherPart.Parent
if otherPart then
local player = game.Players:FindFirstChild(otherPart.Parent.Name)
if otherPart then
game.Players.LocalPlayer.leaderstats.coins.Value = game.Players.LocalPlayer.leaderstats.coins.Value + 1
end
end
end
HitPart.Touched:Connect(onTouch)
HitPart is the part that is touching the other parts. However, I keep getting "attempt to index nil with 'leaderstats'" error. does anyone know whats wrong?
Have you created the leaderstats inside the player? Here's an in-depth article about In-game leaderboards by Roblox
The error is telling you that you are trying to access the leaderstats of a player that doesn't exist.
You have a few issues.
Since this is (probably) a Script, game.Players.LocalPlayer doesn't exist. LocalPlayer only exists in LocalScripts.
Since the Touched signal will fire for any object that touches it, you need to add safety checks that the player object actually exists
To make finding a player easier, I would recommend the Players:GetPlayerFromCharacter function. You pass it a Model from the Workspace, and it tells you if you've got a Player.
local HitPart = script.Parent
local function onTouch(otherPart)
local characterModel = otherPart.Parent
local player = game.Players:FindPlayerFromCharacter(characterModel)
if player then
local coins = player.leaderstats.coins
coins.Value = coins.Value + 1
end
end
HitPart.Touched:Connect(onTouch)

script works but nothing happens in-game in roblox studio

I am having problems with the functionality of a script and I don't know how to solve it.
In the images below attach photos of the in-game problem, this means that the whole script works and in-game the property of the visible "bframe" is false but in-game this is not shown and I want to know how I can solve it ..
in-game mode the visibility of the "Bframe" it becomes false in the property (that's fine) but in-game this is not seen..
Does anyone know how to solve it?
Button script
-- Events
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Evento = ReplicatedStorage.RemoteEvent
-- end of Events
local button = script.Parent
local frame = script.Parent.Parent.Parent.Frame
Evento.OnClientEvent:Connect(function(argumento1)
button.MouseButton1Click:Connect(function()
if frame.Visible == false then
frame.Visible = true
button.Visible = false
else
frame.Visible = false
end
end)
end)
Server script
-- Events
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Evento = ReplicatedStorage.RemoteEvent
-- end of Events
game.Players.PlayerAdded:Connect(function(player)
Evento:FireClient(player,"argumento1")
print("testing probado")
end)
Redteam / touch part script called script
local wll = script.Parent.Part
local fr = game.StarterGui.TeamChangerGui.BFrame
local function Toch()
fr.Visible = false
fr.button.Visible = false
print("visible desactivado")
end
wll.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
Toch()
end
end)
**images**
enter image description here
enter image description here
enter image description here
Is the button script a localscript? if it is then it may be to do with the fireclient although I am not good at it and I usually use fireallclients passing through the player name then checking if its the correct name on the clients side script.
Otherwise I am not sure as it is an interesting issue i have never encountered. Hope it works!

Sound plays when I click on an object

I can't get a sound to play when I click the part.
How can I integrate the sound code into the following code?
local ClickDetector = script.Parent.ClickDetector
ClickDetector.MouseClick:Connect(function()
script.Parent.SurfaceLight1.Enabled = not script.Parent.SurfaceLight1.Enabled
end)
ClickDetector.MouseClick:Connect(function()
script.Parent.SurfaceLight2.Enabled = not script.Parent.SurfaceLight2.Enabled
end)
ClickDetector.MouseClick:Connect(function()
script.Parent.SurfaceLight3.Enabled = not script.Parent.SurfaceLight3.Enabled
end)
ClickDetector.MouseClick:Connect(function()
script.Parent.SurfaceLight4.Enabled = not script.Parent.SurfaceLight4.Enabled
end)
I see you're an RLua developer. Could you make a local of the sound?
By the way, a client-side script would look like this:
local Sound = workspace.Sound
ClickDetector.MouseClick:Connect(function()
Sound:Play()
end)
Well, you can make a remote that is fired when the button is clicked to make it server-side (everyone hears it). Make a remote, add a script (not local) anywhere and the code should be like:
local Sound = workspace.Sound
local RemoteEvent = game.ReplicatedStorage.PlaySound
RemoteEvent.OnServerEvent:Connect(function()
Sound:Play() -- Yes, it's all LOL
end)
I forgot to tell that all your code is client-side, and other players won't see the lights changing, but... if your game has too many customizable remotes (like you make a remote that fires kill for admin, then it can be abused by hackers), so it's not the best - I am not going to lie. A game can't have a single hack.
You can try this code
local function onMouseClick(Player)
if not Player.PlayerGui:findFirstChild("ClickAudio") then
local ls = Instance.new("Sound")
local length = 1 -- How long the sound plays before removed.
ls.SoundId = "http://www.roblox.com/asset/?id=" -- Audio id here.
ls.Name = "ClickAudio"
ls.Volume = 0.5 -- You can set the volume (Suggestion: keep it below 1)
ls.Pitch = 1 -- This is the pitch of the sound
ls.Parent = Player.PlayerGui
ls:Play()
game:GetService("Debris"):AddItem(ls, length)
end
end
script.Parent.MouseClick:connect(onMouseClick)
You would have to change the SoundId = "" and paste in the link of the sound you want to play, I hope this answered your question!

Removing a player's leaderstats if they click a gui on roblox

Some simple code
script.Parent.MouseButton1Up:connect(function()
????.leaderstats.lvl.Value = 0
????.leaderstats.xp.Value = 0
????.leaderstats.gold.Value = 0
It's not even working. So the player clicks the gui, but how can it reset the players leaderstats, specifically lvl, xp, and gold, I run a fairly popular roblox rpg game with about 400 people right now and this would be an immense help.
You could put the following code in a LocalScript inside your button.
Player = game.Players.LocalPlayer --Only works inside a localscript, replace with the line below it if you need it as a Script.
-- Player=script.Parent while (not Player:IsA("Player")) do Player = Player.Parent end
script.Parent.MouseButton1Up:connect(function()
local index, stat
for index, stat in pairs(Player.leaderstats:GetChildren()) do
stat.Value = 0
end
end)
It should be in a LocalScript, so you can just use the LocalPlayer variable to get the leaderstats and set them.

Resources