What is wrong with my script?

  1. What do you want to achieve?
    I’ve been trying for hours to rewrite my script for it to work & not have any errors; however, everything I seem to try doesn’t work.

What I want to happen is if the player touches a part with a tool while the BoolValue = true, they receive money and the tool is removed from their inventory and goes back to ServerStorage.

  1. What is the issue?
local tool = game.ServerStorage["Straightener"]
local giver = game.Workspace.StraightenerP
local prp = script.Parent
local customer = prp.Parent

script.Parent.Touched:Connect(function(Hit)
	if Hit.Parent.Name == 'Straightener' and 
		if game.StarterPlayer["Straight"].Value == true then
		player.leaderstats.Money.Value == player.leaderstats.Money.Value + 25
			game.StarterPlayer["Straight"].Value == false
			tool.Parent = game.ServerStorage

		end
	end)

This ServerScript is in the part that the player has to touch with the tool.

I’m very new to scripting so I’m sorry if my script looks like straight garbage :sweat_smile:

1 Like

There should only be one equal sign if its not a condition(like if statements) in your script

Are there errors in the ouput?

== is for checking, = is for setting.
Change your == to =, since you’re setting the value.
Also, you can use Value += Amount instead of Value = Value + Amount.

Also, you need to define player, you can use the Players:GetPlayerFromCharacter function.
You need to define your tool as well.

On top of that, you are modifying the StarterGui, you need to modify the player’s PlayerGui, and this should be done on the client-side. And again, you are using == instead of =.

1 Like

as you can see roblox studio editor underlines errors automatically so you can just hover your mouse over it and see the error in detail

Screen Shot 2022-07-18 at 1.47.02 PM

You need a PlayerObject, which you can get from the Tool’s Parent and using PlayerService:GetPlayerFromCharacter()
Make sure to wrap that in a variable and to define PlayerService with game:GetService(“Players”)

== is for reading, = is for putting a new value, like @Syclya said

1 Like

No errors in the Output (thank you for asking though, I forgot to check)

I also revised the script but I’m still getting errors :confused:
Screen Shot 2022-07-18 at 1.54.12 PM

local tool = game.ServerStorage["Straightener"]
local giver = game.Workspace.StraightenerP
local prp = script.Parent
local customer = prp.Parent

script.Parent.Touched:Connect(function(Hit)
	if Hit.Parent.Name = 'Straightener' and game.StarterPlayer["Straight"].Value = true then
		game.Players:GetPlayerFromCharacter("leaderstats").Money.Value += 25
			game.StarterPlayer["Straight"].Value = false
			tool.Parent = game.ServerStorage

		end
	end)

Instead of using

:GetPlayerFromCharacter()

You should use:

:FindFirstChild()

or

:WaitForChild()

The tool is parented to server storage but it’s supposed to be parented to workspace no?

You can’t get a character from leaderstats.

Do Hit.Parent.Parent

1 Like

Guessing it replicates to inside Character as it’s a tool.

Screen Shot 2022-07-18 at 1.54.12 PM
Screen Shot 2022-07-18 at 1.58.18 PM

local tool = game.ServerStorage["Straightener"]
local giver = game.Workspace.StraightenerP
local prp = script.Parent
local customer = prp.Parent

script.Parent.Touched:Connect(function(Hit)
	if Hit.Parent.Name = 'Straightener' and game.StarterPlayer["Straight"].Value = true then
		game.Players:GetPlayerFromCharacter("leaderstats").Money.Value += 25
			game.StarterPlayer["Straight"].Value = false
			tool.Parent = game.ServerStorage

		end
	end)
local tool = game.ServerStorage["Straightener"]
local giver = game.Workspace.StraightenerP
local prp = script.Parent
local customer = prp.Parent

script.Parent.Touched:Connect(function(Hit)
	if Hit.Parent.Name == 'Straightener' and game.StarterPlayer["Straight"].Value == true then
		game.Players:GetPlayerFromCharacter(Hit.Parent.Parent).leaderstats.Money.Value += 25
			game.StarterPlayer["Straight"].Value = false
			tool.Parent = game.ServerStorage

		end
	end)

Honestly, I would recommend you clone the tool outside, and destroy it when finished instead of using the same tool

2 Likes

If you want to check if something is equal to something else then you use 2 equals signs like this: ==
If you set a value to something you use only one equal sign. If you want to get a player from the character with your script then simply do local Character = game.Players:GetPlayerFromCharacter(Hit.Parent.Parent) ← this only works IF the Hit part is a direct child of the character though so you want to check if the Character is nil, so you can do “local Character = game.Players:GetPlayerFromCharacter(Hit.Parent.Parent) if Character ~= nil then … your code here … end”

The tool name check does already this, and since tools are inside character, it is valid and can be expected that the character is outside and free to be used.

1 Like

This will error if the player doesn’t exist, check if the player actually exists before attempting to just set the value out of nowhere.

local Player = PlayerService:GetPlayerFromCharacter(hit.Parent)
if (Player) then
   -- Code.
end

Technically, the players character is existant which means theyre in the game, however this would be a case if the original poster’s scripting style was different.

Overall a good point, but I do what the original poster askes.

This will run whenever the .Touched event fires, it can fire if it touches another part (that is not a descendant of the player instance), which will then result in an error.

Just letting you know as people might see this topic in the future.

Well of course. And yes, I do know these basics after being a scripter for years. However, I usually do what the original poster wants to do and keep their own style so they can learn from it.

1 Like