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)
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.
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)
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