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

Math equation to figure out a certain price in a script?

Asked by 5 years ago

Well, i need some help if anyone can. I've been trying for the past 30 minutes to get this working but it doesn't. I've been trying to make a script where when a player touches a part, the script takes a leader stat value from the player and multiples it by a certain value (like 1 or 4) and gives them the amount calculated.

So far I've come up with this

local player = game.Players
local Players = game.Players
local G = player:FindFirstChild("leaderstats").leaderstats:FindFirstChild("Grass").Value
local total = (G * 1)
script.Parent.Touched:Connect(function(hit)
    workspace.RemoteEvent:FireServer(player.Name)
    if player:FindFirstChild("leaderstats")
     and player.leaderstats:FindFirstChild("Grass") then
         player.leaderstats.Grass.Value = player.leaderstats.Grass.Value + total        
    end
end)

Although I've tried this many times i had no choice but to ask for help, in this script sofar Line 3 is showing "Attempt to Index a nil value" If anyone could help me i would be so thankful.

0
You realize that player is game.Players and not a specific player, right? little5 2 — 5y
0
Yes i do realise that after looking at the script, Im trying to direct it to a player but theres parts that still break. Im noy particularly best with coding but im assuming do i use this in a local script to define game.Players("localplayer")? RebornedInFire 35 — 5y
0
I believe my answer should help @RebornedInFire Ziffixture 6913 — 5y

1 answer

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

You're not targeting the actual Player, instead, you're targeting the Service that handles all the Players. This means you're trying to access a Folder within a Service meant to be a descendant of Child. Consider changing line 1 to

local Player = game:GetService("Players").LocalPlayer --// would be defined with '.'
local Grass = Player:WaitForChild("leaderstats").Grass
Grass.Value = (Grass.Value * 1)

If you're trying to go through every player, and this Isn't a LocalScript, consider using a pairs loop to Iterate through all the Players. There are a few other functions too like next, for example, check those out while you're at it

local Players = game:GetService("Players"):GetPlayers()
for p in ipairs(Players) do --// ipairs may be a bit confusing, if so, work with pairs instead.
    local Grass = Players[p]:WaitForChild("leaderstats").Grass
    Grass.Value = (Grass.Value * 1)
end

I also noticed you passed player.Name as an argument towards the Server, this is automatically done, therefore it is unneeded, don't forget to reference the Player Variable through first when listening for the Event.

Hope this helps! don't forget to accept and upvote!

0
Hey so, the reason behind me setting the G * 1 is this is a sell point. I want G to multiply by 1 cause the value of the Grass is 1. Also, i want to diretly go to the players Leaderstats and one of their values and make it as a local. Something like local G = player:leaderstats:(FindFirstChild)(IntValue) RebornedInFire 35 — 5y
0
As the fact that, G = player:leaderstats:(FindFirstChild)(The Int Value)(The Value of the int value) like the value of a certain players leaderstat. And take that, multiply it by 1 to the value of the int value IntValue = 7 so like 1 * 7 = 7, and this 7 is added to the players cash leaderstat. RebornedInFire 35 — 5y
0
Next is not a method. PLEASE use proper terminology. And Players is not a folder either User#24403 69 — 5y
0
Thanks for the help, although i cant directly access the IntValue of a Leaderstat in a person, i've had a lot of help so ill be on my way to do some more practicing on this and configuring.I have accepted it therefore :) RebornedInFire 35 — 5y
Ad

Answer this question