ok so im a beginner scripter and im trying to make a single player tycoon. right now im trying to make a script that adds cash directly from ore to player . i was brainstorming all day how to make it work.
local furnace = script.Parent wait(1) local plrname = game.Workspace.PlayerName.Value furnace.Touched:Connect(function(hit) if hit.Cash ~= nil then local player = game.Players.plrname multiplier = script.Parent.Parent.Multiplier.Value wait(0.1) hit.BrickColor = BrickColor.new("Dark stone grey") wait(0.1) hit.BrickColor = BrickColor.new("Really black") wait(0.1) player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + hit.Cash.Value * multiplier hit:Destroy() end end)
plrname is the stringvalue which shows player name
error im getting: 11:07:12.722 - plrname is not a valid member of Players
does anyone know how to fix it?
Consider FindFirstChild()
for your situation.
Instance:FindFirstChild()
has two parameters:
The name of the child to look for (string)
Recursive searching (boolean). If set to true, FindFirstChild()
will search recursively, meaning that it will search through all descendants of Instance
to find the given child. (This parameter is optional and is set to false
by default.)
FindFirstChild()
will do one of two things:
If a child of given name is found, FindFirstChild()
returns the userdata with that name.
If no child of given name is found, FindFirstChild()
returns nil
.
As a safety net, line 5 should be:
if hit:FindFirstChild("Cash") then
Then you'd change line 6:
local player = game.Players:FindFirstChild(plrname)
Since plrname
is a string, it would only make logical sense to use a method that takes a string.
Better yet, if a player touches the part, you can acquire it using GetPlayerFromCharacter()
:
if game.Players:GetPlayerFromCharacter(hit.Parent) then
If the condition passes, it means that a player touched the part.
Simple fix.
game.Workspace.PlayerName.Value is what is breaking your script. You want to change it to
local plrname = game.Players.LocalPlayer.Name
This is because the "players" group is outside of "workspace".
Heres a tip im not guranteed itll work but based on what u described all u really need to know is the owner of the tycoon right? so first u should make the tycoon door owner and when a player touches it do this script like the touch to be owner doors they usually make
script.Parent.Touched:connect(function(part) local player = Part.parent.parent -- this will tell the game the players name wont work if the character has a hat or accesory if he does u can do -- these 3 lines the local player and if not is optional u can either use the one above or this if players have hats or accesorys i recommend the one above as this may not work local player = part.parent.parent if not player then local player = part.parent.parent.parent script.Parent.Transparency = 0.5 script.Parent.CanCollide = false -- then for the other script u will do this like when the ore touches the pad btw put this all in a single script end end) game.workspace.(OreName).Touched:connect(function(part) if part = game.workspace.(CashPadName) then local multiplier = script.Parent.Parent.Multiplier.Value wait (0.1) hit.Brickcolor = Brickcolor.new("Dark stone grey") wait (0.1) hit.Brickcolor = Brickcolor.new("Really Black") player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + part.Cash.Value * multiplier part:Destroy() end end) -- this should be local script btw and also change the orename and cashpadname into the thing and put the script in the area i hope this helped