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

How to change a value using localscript?

Asked by 3 years ago

So I am fairly new to scripting and I have made a simple code where you click on a stick and it increases a value found in the local player's PlayerScripts named "stickvalue" by 1 but it is doing absolutely nothing the code is:

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local clickdetector = script.Parent.ClickDetector
local stickvalue = game.Players.LocalPlayer.PlayerScripts.stickvalue.Value
clickdetector.MouseClick:Connect(function(player)
        stickvalue = stickvalue + 1
end)
0
can you show the errors in the dev console (if there is any)? NGC4637 602 — 3y
0
actually nvm i think i see the problem NGC4637 602 — 3y

3 answers

Log in to vote
0
Answered by 3 years ago

Can you show me where the stickvalue IntValue is created? and also, you can do "stickvalue += 1" to add one to the current value, and you can do "stickvalue -= 1" to take away 1 from the current value.

Ad
Log in to vote
0
Answered by
NGC4637 602 Moderation Voter
3 years ago
Edited 3 years ago

Judging from the clickdetector part, its safe to say you put the local script in workspace. That is plain wrong. local scripts cannot work outside the localplayer, or the character of the local player.

Instead, use a server script. MouseClick function has a parameter that helps you here and that parameter is called playerWhoClicked (you can change to player, like you did in your local script, or you can change to something funny like stopusingscriptviewer).

so, put a server script (normal script) at where you put the local script, and in the script, put the following code:

local clickdetector = script.Parent.ClickDetector
game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder",player)
    leaderstats.Name = leaderstats

    local stickvalue = Instance.new("DoubleConstrainedValue",leaderstats)
    stickvalue.Name = Sticks
    stickvalue.MinValue = 0
    stickvalue.MaxValue = 1e303
end)
clickdetector.MouseClick:Connect(function(stopusingscriptviewerdumbo) -- casually trolling dex users with parameters
    local stickvalue = stopusingscriptviewerdumbo.leaderstats.Sticks.Value -- same thing but we use the funni parameter as a reference to the player since server scripts can't access local player.
    stickvalue = stickvalue + 1

also, local Character and local Humanoid was useless since you didn't even use the humanoid or character.

lemme say this again. **Don't use local scripts, use normal scripts, or else this will not work. **

0
if stickvalue is in a folder called leaderstats (which you should put the stickvalue in), change PlayerScripts (at line 3) to leaderstats. NGC4637 602 — 3y
0
in case you were using a server script, sadly you cannot do game.Players.LocalPlayer in server scripts. they will detect nothin since to the server script, local player is the server and since the server is not a player, it returns nil (nothing) NGC4637 602 — 3y
0
It did not work, the value still refuses to go up ThatOneLittleNoob 2 — 3y
0
could you perhaps write a different code entirely that you think would work instead of editing my original one ThatOneLittleNoob 2 — 3y
View all comments (2 more)
0
the code put a "Sticks" column on the leaderboard the value still refuses to go up ThatOneLittleNoob 2 — 3y
0
heeeeh i found a way ThatOneLittleNoob 2 — 3y
Log in to vote
0
Answered by 3 years ago

Why wait for somebody to rescue you from a tower when you can rescue yourself

I found a simple way

local clickdetector = script.Parent.ClickDetector
clickdetector.MouseClick:Connect(function(player)
    player.Character.stickvalue.Value += 1
end)

i then put a stickvalue IntValue inside the StarterCharacterScripts folder and now when you click on the stick it works. ez

0
is that a local script? NGC4637 602 — 3y
0
try and create a 2 player server in studio (in test), try to do your thing in player 1 then switch to player 2, see if player 2 sees the result. NGC4637 602 — 3y
0
if player 2 doesn't see player 1's stats add, you will know what I mean by FE but i'll say it again. You know why people say FE ruined many games? (which it did), it is because FE made it so that local scripts cannot directly talk to the server, this is to stop hackers from changing their value to 9999999 and getting away with it. basically the rule of FE is never trust the client. NGC4637 602 — 3y
0
local scripts cannot be used to change values because it is not replicated, meaning if you change a value with a local script, it will change for you but it will not change for other players, nor will it change for the server. It's complicated but you'll understand NGC4637 602 — 3y
View all comments (2 more)
0
i'll stop, since it is impossible to convince a novice NGC4637 602 — 3y
0
its not about local scripts, i already know that, im saying that i put the intvalue in the startercharacterscripts, then got the stick to update the value INSIDE the player model with a serverscript. got my friend on and it works exactly as planned ThatOneLittleNoob 2 — 3y

Answer this question