I am working on a school project and have been scripting a small rover exploration game. Everything I have done has worked up to this point. I am trying to find a solution to this problem, and it looks to be related to my script
I have tried rearranging the function, I have delved into my leader stats, I have cross examined with other working code, I have changed input types (button.Activated, button.MouseButton1Click, etc). Nothing seems to work.
You didn’t specify the parameter player when you called the function rightclick(), so by default it’s nil
Since it’s a local script, use game.Players.LocalPlayer instead
You’ll also have to change your current script into a LocalScript in order to properly obtain the Player, but keep in mind any changes made to the client will only be visible from your local POV (Point of View)
It’d be better to reference the Player Object outside your rightClick function, and make sure to call WaitForChild() as well to ensure that the object you’re looking for is completely valid
And if you worry about performance, make your function “local” as it’ll be ~10% quicker when opposed to using a global function
And please make sure to indent your Coding, like this for future posts
` ` ` -- Add 3 backticks here...
local String = "This is a code block!"
` ` ` -- And another 3 here, to end your code!
A better overview on how it should look:
local button = script.Parent
local frame = button.Parent
local gui = frame.Parent
local sign = gui.Parent
local bridge = workspace:WaitForChild("Bridge")
local upgrade = workspace:WaitForChild("BPUpgrade")
local Plr = game.Players.LocalPlayer
locla playerStats = Plr:WaitForChild("leaderstats")
local playerMaterials = playerStats:WaitForChild("Crystals")
local playerAccount = playerStats:WaitForChild("Account")
local function rightclick()
local vector = Vector3.new(bridge.Position.X + 100, bridge.Position.Y + 100, bridge.Position.z + 100)
if playerAccount.Value >= upgrade.Value then
-- Do your code stuff here
end
end
button.MouseButton1Click:Connect(rightclick)
Yeah, as @Jackscarlett said, MouseButton1Click does not return any paramters. My suggestion would be to try this:
local button = script.Parent
local bridge = game.Workspace.Bridge
local upgrade = game.Workspace.BPUpgrade
button.MouseButton1Click:Connect(function()
local playerstats = game.Players.LocalPlayer:WaitForChild("leaderstats")
local playerAccount = playerstats.Account
local vector = Vector3.new(bridge.Position.X+100, bridge.Position.Y+100, bridge.Position.Z+100)
if playerAccount.Value >= upgrade.Value then
--you should add your code here
end
end)
P.S. You will need to change this script to a local script
This is essentially what @Jackscarlett said but I removed some of the unneeded variables
After trying this I am still not getting any functionality. I have tried all of the replies on this post and now I don’t get any errors, but I also don’t get any response from the code itself. It is attempting to connect to a leaderboard and its files that are created from with the ServerScriptService. The files then are under the player when they join. (I forgot to really express that earlier, sorry)
Can you add some print() statements to debug the issue, and open up your Console to figure out what the issue is? Depending on how your have your game hierarchy sorted out, we need to focus on each individual part then if we wanna resolve the issue here
It wouldn’t make sense that nothing’s working if the script I provided you should work fine
local button = script.Parent
local bridge = game.Workspace.Bridge
local upgrade = game.Workspace.BPUpgrade
button.MouseButton1Click:Connect(function()
local playerstats = game.Players.LocalPlayer:WaitForChild("leaderstats")
local playerAccount = playerstats.Account
local vector = Vector3.new(bridge.Position.X+100, bridge.Position.Y+100, bridge.Position.Z+100)
print("Hello")
end)
You need to wait for the leaderstats folder by using :WaitForChild(), because the folder was not existed / was nil at that time!! Make sure the script inside of the button or else it not gonna work!!
Code:
local player = game:GetService('Players').LocalPlayer
function rightclick()
print('Button Clicked')
local playerStat = player:WaitForChild('leaderstats')
local playerMaterials = playerStat.Crystals
local playerAccount = playerStat.Account
local vector = Vector3.new(bridge.Position.X + 100, bridge.Position.Y + 100, bridge.Position.Z + 100)
if playerAccount.Value >= upgrade.Value then
--- do something here to make it work!!
print('PlayerAccount value is larger than upgrade value!!')
end
end
script.Parent.MouseButton1Click:Connect(rightclick)
Are you using a local script in the workspace? Try either moving the SurfaceGui to the StarterGui and adorning it to the part, or use a normal script with RunContext set to Client. LocalScripts only run in a few scenarios:
I tried with both a local script and a normal script. The normal script gave me output but with the following message from the beginning:
I am fairly new with Lua, and I might be missing something obvious. The code that gave me the above error is yours, I did not edit it other than adding where you said
I think I ended up fixing it, thank you. I hadn’t changed the RunContext to Client. After changing it the problem seemed to be fixed. I will come back to this thread if more problems appear. Thank you all for your help
local Player = game:GetService('Players').LocalPlayer
function rightClick()
for _, leaderstats in pairs(Player:GetChildren()) do
if v:IsA('Folder') and v.Name == 'leaderstats' then
local playerMaterials = leaderstats.Crystals
local playerAccount = leaderstats.Account
local vector = Vector3.new(bridge.Position.X + 100, bridge.Position.Y + 100, bridge.Position.Z + 100)
if playerAccount.Value >= upgrade.Value then
end
else
print('Folder is not existed!!')
end
end
end
script.Parent.MouseButton1Click:Connect(rightClick)