Potion Giver isn't working

Hello! So I’m trying to make it so a NPC will give the player a potion, The script isn’t working though! here is the Process!

No.1 - Player Agrees to Getting The Potion

local SpeedPotion = game.ReplicatedStorage.SpeedPotion

local Dialog = game.Workspace.Wizard_Noob.Dialog
Dialog.DialogChoiceSelected:Connect(function(Player,Choice)
	if Choice.Name == "GivePotion" then
		local SpeedPotionClone = SpeedPotion:Clone()
		SpeedPotionClone.Name = "Speed Potion"
		SpeedPotionClone.Parent = game.Workspace
		SpeedPotionClone.Position = Vector3.new(-10.47, 27, -255.549)
		local PotionActivaterClone = SpeedPotion.PotionActivater:Clone()
		PotionActivaterClone.Name = "PotionActivater"
		PotionActivaterClone.Parent = SpeedPotionClone
	else
		if Choice.Name == "DontGivePotion" then
			return
		end
	end
end)

No.2 - Player Touches The Potion

local function PotionActivate(hit)
	local humanoid = hit.Parent.Humanoid
	if humanoid ~= nil then
		humanoid.WalkSpeed = 40
		local sprkls = Instance.new("Sparkles")
		sprkls.Name = "Effect"
		sprkls.Parent = script.Parent
		script.Parent:Destroy()
	else
		if humanoid == nil then
			return
		end
	end
end

script.Parent.Touched:Connect(PotionActivate)

Now, No.1 works out well but No.2 whenever I touch the Potion then, for some reason the Potion has No Affect, Here is the script that is supposed to boost the player

local function PotionActivate(hit)
	local humanoid = hit.Parent.Humanoid
	if humanoid ~= nil then
		humanoid.WalkSpeed = 40
		local sprkls = Instance.new("Sparkles")
		sprkls.Name = "Effect"
		sprkls.Parent = script.Parent
		script.Parent:Destroy()
	else
		if humanoid == nil then
			return
		end
	end
end

script.Parent.Touched:Connect(PotionActivate)
2 Likes

Is there any console errors we may need to know about?

2 Likes

Nope! Not at all that I’ve seen!

2 Likes

Is it possible that the second script’s Disabled property is set to true? Your script seems to work otherwise.

3 Likes

I tested it and it seems to be working fine, is this a normal script or a local script?

2 Likes

First of all, when dealing with the character, use WaitForChild. That may be the problem.

1 Like

No

The First Script is Local

Second Script is Just Normal

Which Script and Were?

1 Like

The second script. Where local humanoid = hit.Parent.Humanoid

1 Like

The First Script is Local

Second Script is Just Normal

There’s your problem. Making the first script a regular one should fix it.

This is because you’re copying the script on the client’s side, so it’s not replicated to the server. Because it’s not replicated, it’s not run. Read more at developer.roblox.com.

2 Likes

But this is the only way that it will work! If its not a local script then It wont work!

1 Like

I think you would have to use remote events then

1 Like

Have you tried that already or are you doing it now?
An object is supposed to be cloned in the server instead of a client.

1 Like

Sorry, you’re right. You need to use a RemoteEvent if that’s the case.

First, add a RemoteEvent named PotionEvent to game.ReplicatedStorage.

Then, add this as a regular script to either workspace or game.ServerScriptService:

--This is a normal Script.
local PotionEvent = game:GetService("ReplicatedStorage"):WaitForChild("PotionEvent")
local SpeedPotion = game.ReplicatedStorage.SpeedPotion

function GivePotion(player)
	local SpeedPotionClone = SpeedPotion:Clone()
	SpeedPotionClone.Name = "Speed Potion"
	SpeedPotionClone.Parent = game.Workspace
	SpeedPotionClone.Position = Vector3.new(-10.47, 27, -255.549)
	local PotionActivaterClone = SpeedPotion.PotionActivater:Clone()
	PotionActivaterClone.Name = "PotionActivater"
	PotionActivaterClone.Parent = SpeedPotionClone
end

PotionEvent.OnServerEvent:Connect(GivePotion)

Also, change the LocalScript to this:

--This is the LocalScript.
local PotionEvent = game:GetService("ReplicatedStorage"):WaitForChild("PotionEvent")

local Dialog = game.Workspace.Wizard_Noob.Dialog
Dialog.DialogChoiceSelected:Connect(function(Player,Choice)
	if Choice.Name == "GivePotion" then
		PotionEvent:FireServer()
	else
		if Choice.Name == "DontGivePotion" then
			return
		end
	end
end)
1 Like

I have some other suggestions.

1- Instead of:

local humanoid = hit.Parent.Humanoid

do:

local humanoid = hit.Parent:FindFirstChild("Humanoid")

In your example, if no Humanoid is found, the script will throw an error and halt. On the other hand, FindFirstChild returns nil but does not immediately throw an error, so you can later check if the instance has been found. In this case, this does not break your script, however, I’m sure you’ll have cases where it does cause problems.

2- Instead of:

if x == 1 then
	print("x is 1")
else
	if x == 2 then
		print("x is 2")
	end
end

You can do:

if x == 1 then
	print("x is 1")
elseif x == 2 then
	print("x is 2")
else
	print("x is not 1 or 2")
end
1 Like

It works, thank you for the help!

1 Like