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

Anybody know what's wrong with this function script, I can't find the error?

Asked by 7 years ago

I'm trying to make a script that makes a gui appear only if a player has surpassed a certain leaderstat, this is the code I have so far and there's nothing wrong with the Parents or anything like that so I can't find my error, any help?

player = script.Parent.Parent.Parent.Parent
stats = player:WaitForChild("leaderstats")
Level = stats:WaitForChild("Level")

game.Players.PlayerAdded:connect(function()
if Level.Value >= 50 then
script.Parent.Visible = true
end
end)

0
BTW the parent is a frame so the visible = true should work greybird700 63 — 7y
0
The script in which this code resides probably loads after the player has been added to the game, so the PlayerAdded event won't fire. I'd suggest putting this in a LocalScript and doing the level check directly rather than waiting for an event. duckwit 1404 — 7y
0
How would I do it directly, can you give an example using my code? greybird700 63 — 7y
0
I think I see it, look at what I write and see if it works Gwenz3 0 — 7y

2 answers

Log in to vote
0
Answered by
Gwenz3 0
7 years ago
Edited 7 years ago
1:  player = script.Parent.Parent.Parent.Parent
2: stats = player:WaitForChild("leaderstats")
3: Level = stats:WaitForChild("Level")
4: game.Players.PlayerAdded:connect(function()
5: if Level.Value = 50 then
6: script.Parent.Visible = true
7: end
8: end)

You messed up and wrote a > which messes up the script

0
Try using this script Gwenz3 0 — 7y
0
No, that was on purpose, it means greater than or equal to, thanks for trying though :) greybird700 63 — 7y
0
You also are supposed to do: == when it comes to if then statements. Vingam_Securis 213 — 7y
Ad
Log in to vote
0
Answered by
Azarth 3141 Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

You're trying to index player and are also using PlayerAdded. That tells me you're trying to use this in a LocalScript in PlayerGui. You can't use PlayerAdded in this way. You can use ChildAdded, but either way, what you seem to be doing isn't going to work.

This needs to be in a LocalScript

local players = game:GetService("Players")
local player = players.LocalPlayer
local stats = player:WaitForChild("leaderstats")
local Level = stats:findFirstChild("Level")

if not Level then print("Can't find level") return end
if Level.Value >= 50 then  -- First check to see each time we respawn with the GUI
    script.Parent.Visible = true
end

Level.Changed:connect(function() -- Every check after level changes
    if Level.Value >= 50 and not script.Parent.Visible then 
        script.Parent.Visible = true
    end
end)

0
Thanks so much for all of this and I know this is going to sound annoying but this still doesn't work and I can't find an error with what you gave me like it makes no sense as to why it doesn't work greybird700 63 — 7y

Answer this question